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())
    //解释:通过创建新的对象,不再是同一个,再指定构造函数属性为自己
    
    
  • 相关阅读:
    CSS的扩展less和sass
    html5小游戏基础知识
    htm5拖放和画布
    htm5
    并查集模板
    二叉树的建树
    kmp的书写
    贪心算法
    容器
    POJ2442 优先队列
  • 原文地址:https://www.cnblogs.com/junhey/p/8947214.html
Copyright © 2011-2022 走看看