美文网首页我爱编程
javascript继承,es6继承,node.js继承总结

javascript继承,es6继承,node.js继承总结

作者: JamHsiao_aaa4 | 来源:发表于2017-07-08 15:59 被阅读0次

在高级语言中,面向对象是个很重要的概念。一提到面向对象,我们都会想到三大特征——封装,继承,多态。

本文详解javascript继承,es6继承,node.js继承

javascript继承:

  • 由于js不像java那样是真正面向对象的语言,js是基于对象的,它没有类的概念
  • 所以,要想实现继承,可以用js的原型prototype机制或者用apply和call方法去实现。在面向对象的语言中,我们使用类来创建一个自定义对象。然而js中所有事物都是对象,那么用什么办法来创建自定义对象呢?
  • 这就需要用到js的原型:我们可以简单的把prototype看做是一个模版,新创建的自定义对象都是这个模版(prototype)的一个拷贝 (实际上不是拷贝而是链接,只不过这种链接是不可见,新实例化的对象内部有一个看不见的 Proto 指针,指向原型对象)。
  • 1、继承第一种方式:对象冒充

    blogfunction Parent(username){
        this.username = username;
        this.hello = function(){
          console.log('hello ' + this.username);
        }
      }
    Parent.prototype.sayMorning = function(){
    	console.log('good morning ' + this.username);
    }
      function Child(username,password){
        //通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承
        //第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,
        //第二步:执行this.method方法,即执行Parent所指向的对象函数
        //第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法
        this.method = Parent;
        this.method(username);//最关键的一行
        delete this.method;
    
         this.password = password;
        this.world = function(){
          console.log(this.password);
        }
      }
    
      var parent = new Parent("zhangsan");
      var child = new Child("lisi","123456");
      parent.hello();
      parent.sayMorning();
      child.hello();
      child.world();
    

    2、继承第二种方式:call()方法方式

    function Parent(username){
        this.username = username;
        this.hello = function(){
          console.log(this.username);
        }
      }
    
    Parent.prototype.sayMorning = function(){
    		console.log('good morning ' + this.username);
    	}
    
      function Child(username,password){
        Parent.call(this,username);
    
        this.password = password;
        this.world = function(){
          console.log(this.password);
        }
      }
    
    
      var parent = new Parent("zhangsan");
      var child = new Child("lisi","123456");
      parent.hello();
      parent.sayMorning();
      child.hello();
      child.world();
    // child.sayMorning();  通过prototype 添加的方法和属性,不能用来继承
    

    3、继承的第三种方式:apply()方法方式

    function Parent(username){
        this.username = username;
        this.hello = function(){
          console.log(this.username);
        }
      }
    
    	Parent.prototype.sayMorning = function(){
    		console.log('good morning ' + this.username);
    	}
    
      function Child(username,password){
        Parent.apply(this,new Array(username));
    
        this.password = password;
        this.world = function(){
          console.log(this.password);
        }
      }
    var parent = new Parent("zhangsan");
      var child = new Child("lisi","123456");
      parent.hello();
      parent.sayMorning();
      child.hello();
      child.world();
    // child.sayMorning(); 通过prototype 添加的方法和属性,不能用来继承
    

    4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承

    function Person(){
      }
      Person.prototype.hello = "hello";
      Person.prototype.sayHello = function(){
        console.log(this.hello);
      }
    
      function Child(){
      }
      Child.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承
      Child.prototype.world = "world";
      Child.prototype.sayWorld = function(){
        console.log(this.world);
      }
    
      var c = new Child();
      c.sayHello();
      c.sayWorld();
    通过prototype 添加的方法和属性,不能用来继承
    

    5、继承的第五种方式:混合方式, 混合了call或者apply方式、原型链方式

    function Parent(hello){
        this.hello = hello;
      }
      Parent.prototype.sayHello = function(){
        console.log(this.hello);
      }
    
      function Child(hello,world){
        Parent.call(this,hello);//将父类的属性继承过来
        this.world = world;//新增一些属性
      }
    
      Child.prototype = new Parent();//将父类的方法继承过来
    
      Child.prototype.sayWorld = function(){//新增一些方法
        console.log(this.world);
      }
    
      var c = new Child("zhangsan","lisi");
      c.sayHello();
      c.sayWorld();
    可以继承通过prototype 添加的方法和属性

    es6继承:

    一、类(Class)

    1.基本语法

    JavaScript语言的传统方法是通过构造函数,定义并生成新对象。下面是一个例子

    ? 1 2 3 4 5 6 7 8 function Point(x, y) {   this.x = x;   this.y = y; } Point.prototype.toString = function () {   return '(' + this.x + ', ' + this.y + ')'; }; var p = new Point(1, 2);

    ES6提供了更接近传统语言的写法,引入了Class(类)这个概念,作为对象的模板。通过class关键字,可以定义类。基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。上面的代码用ES6的“类”改写,就是下面这样。

    ? 1 2 3 4 5 6 7 8 9 10 11 //定义类 class Point {   constructor(x, y) {  //constructor 构造方法     this.x = x;     this.y = y;   }   toString() {     return '(' + this.x + ', ' + this.y + ')';   } } var p = new Point(1, 2);

    构造函数的prototype属性,在ES6的“类”上面继续存在。事实上,类的所有方法都还是定义在类的prototype属性上面。

    2.constructor方法

    constructor方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类必须有constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。

    二、继承(Extends)

    Class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要清晰和方便很多。

    ? 1 2 3 4 5 6 7 8 9 class ColorPoint extends Point {   constructor(x, y, color) {     super(x, y); // 调用父类的constructor(x, y)     this.color = color;   }   toString() {     return this.color + ' ' + super.toString(); // 调用父类的toString()   } }

    上面代码中,constructor方法和toString方法之中,都出现了super关键字,它在这里表示父类的构造函数,用来新建父类的this对象。

    子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。

    node.js 继承

    util是node.js的一个核心模块

    util.inherits(constructor,superConstructor)是一个实现对象间原型继承的函数

        function Superclass(name = 'default') {
            this.name = name;
        }
    
        function Subclass(age = 22, name) {
            this.age = 22;
            Superclass.call(this, name);
    
            this.say = function () {
                console.log(`this.name=${this.name}...this.age=${this.age}`);
            };
    
        }
    
        require('util').inherits(Subclass, Superclass);
    
        const test = new Subclass(23);
        test.say();
        console.log(`test instanceof Subclass`, test instanceof Subclass);
        console.log(`test instanceof Superclass`, test instanceof Superclass);
        console.log(test.__proto__.constructor);
        console.log(Subclass.prototype.constructor);

    相关文章

    • javascript继承,es6继承,node.js继承总结

      在高级语言中,面向对象是个很重要的概念。一提到面向对象,我们都会想到三大特征——封装,继承,多态。 本文详解jav...

    • Javascript原型和原型链

      JavaScript在ES6之前没有类似class,extend的继承机制,JavaScript的继承主要是通过原...

    • JavaScript的多态和封装

      在之前的文章中我们已经介绍了JavaScript的继承包含了ES5和ES6的版本。 ES5继承 传送门 ES6继承...

    • 深入JavaScript继承原理

      javascript继承 ES6之前,JavaScript本质上不能算是一门面向对象的语言,因为它对于封装,继承,...

    • js之继承

      文章主讲 JS 继承,包括原型链继承、构造函数继承、组合继承、寄生组合继承、原型式继承、 ES6 继承,以及 多继...

    • 02.javascript中的原型

      本文主要总结javascript 中的继承实现,主要包括原型继承,构造函数继承和组合继承。但在之前还需要巩固一下原...

    • JavaScript--对象创建和继承方法

      JavaScript创建对象方法总结精彩博文javascript继承讲解精彩博文于江水 继承讲解 JavaScri...

    • 继承方式

      es6的继承: es6的继承写法简洁方便,十分直观。 es5的继承:(只推荐组合继承--最常用) 组合继承 除了S...

    • 继承

      老版继承 call () ES6继承 extends 继承super 超类/父类

    • 深入理解javascript中的继承机制 之 12种继承模式总结

      之前我们介绍了多种javascript中的继承方式,最后我们开始总结概括这些继承方式,先将javascript中的...

    网友评论

      本文标题:javascript继承,es6继承,node.js继承总结

      本文链接:https://www.haomeiwen.com/subject/gefjhxtx.html