定义父类和子类的继承关系
//声明父类 function SuperClass(){ this.superValue = true; } //为父类添加共有方法 SuperClass.prototype.getSuperValue=function(){ return this.superValue; } //声明子类 function SubClass(){ this.subValue = false; } //继承父类 SubClass.prototype = new SuperClass(); SubClass.prototype.getSubValue = function(){ return this.subValue; } var instance = new SubClass(); console.log(instance.getSuperValue()); console.log(instance.getSubValue());
console.log(instance instanceof SuperClass);
console.log(instance instanceof SubClass);
console.log(SubClass instanceof SuperClass);
console.log(SubClass.prototype instanceof SuperClass);
console.log(instance instanceof Object);
将父类的实例赋给子类的prototype就实现了 类的继承
类关系的检测
结果如下:
有版权问题请留言,或加我qq362601125
参考列表
1.《JavaScript设计模式》作者张荣铭