JavaScript学习笔记之继承

来源:互联网 发布:印度人是黄种人吗 知乎 编辑:程序博客网 时间:2024/05/01 16:48

        一、原型链实现继承

function SuperType(){      this.property=true;}SuperType.prototype.getSuperValue=function(){      return this.property;};function SubType(){      this.subproperty=false;}SubType.prototype=new SuperType();SubType.prototype.getSubValue=function(){      return this.subproperty;};var instance=new SubType();

        二、使用构造函数实现继承

function SuperType(){      this.colors=["red","yellow"];}function SubType(){      SuperType.call(this);}var instance=new SubType();
        三、使用原型链和构造函数实现组合继承

function SuperType(name){      this.name=name;      this.colors=["red","yellow"];}SuperType.prototype.sayName=function(){      return this.name};function SubType(name,age){      SuperType.call(this,name);      this.age=age;}SubType.protype=new SuperType();SubType.protype.constructor=SubType;SubType.protype.sayAge=function(){      return this.age;};



0 0