标签:color class 属性 类型 子类 hello 不可 完美 定义类
instance检测函数的propertype是否在对象的原型链上出现过
1、借用构造函数
function People(name){ this.name = name } function Student(name){ People.call(this,name) }
缺点:1、instance失效(超类的原型对子类不可见);2、大量重复
2、组合继承
function People(name){
this.name = name } function Student(name){ People.call(this,name) } Student.prototype=new People(‘zale‘)//重写原型,继承父类实例属性与方法 Student.prototype.constructor=Student//由于原型被重写,所以需要重新定义一下constructor,定义类型
缺点:实例属性被赋值了2次
3、原型继承
Object.create(obj)以obj为原型创建对象
缺点:1、注意属性共享;2、没有子类的存在
4、寄生式继承
let People = { name:‘zale‘ } function Student(_prop_){ let st = Object.create(_prop_) st.say = function(){ return ‘hello‘ } return st } let s1 = Student(People)
缺点:同上
5、寄生组合式继承(完美)
function People(name){ this.name = name } function Student(name){ People.call(this,name) } Student.prototype=Object.create(People.prototype)//与组合继承的区别:重新定义原型为父类原型,实例方法这里不会调用 Student.prototype.constructor=Student//由于原型被重写,所以需要重新定义一下constructor
标签:color class 属性 类型 子类 hello 不可 完美 定义类
原文地址:https://www.cnblogs.com/zale-blogs/p/9571638.html