zoukankan      html  css  js  c++  java
  • 继承的例子

    话不多说,直接show代码:

    //构造函数方式
    function Animal { this.name = 'pig' }
    function Animal2 { Animal.call(this); this.age = 18 }
    console.log(new Animal2())
    //缺点:无法继承Animal的原型对象
    
    //原型链方式
    function Animal { this.name = 'pig' }
    function Animal2 { this.age = 18 }
    Animal2.prototype = new Animal();
    console.log(new Animal2())
    //缺点:实际上是共享,修改原型对象里的内容,其它继承的类也会同步修改
    
    //组合方式
    function Animal { this.name = 'pig' }
    function Animal2 { Animal.call(this); this.age = 18 }
    Animal2.prototype = Animal.prototype;
    console.log(new Animal2())
    //缺点:由于引用同一个原型对象,无法区分对象是由谁实例化的
    
    //最终方式
    function Animal { this.name = 'pig' }
    function Animal2 { Animal.call(this); this.age = 18 }
    Animal2.prototype = Obiect.create(Animal.prototype);
    Animal2.prototype.constructor = Animal2;
    console.log(new Animal2())
    //解释:通过创建新的对象,不再是同一个,再指定构造函数属性为自己
    
    
  • 相关阅读:
    Electio Time poj
    排列的字典序问题
    poj 2365
    编程中的命名设计那点事(转)
    编程命名中的7+1个提示(转)
    poj 1664 放苹果——递归
    再论字典序
    poj 3618
    sort用法
    poj 1088
  • 原文地址:https://www.cnblogs.com/junhey/p/8947214.html
Copyright © 2011-2022 走看看