Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

function inheritPrototype(subtype,supertype){
  var prototype = Object(supertype.prototype);
  var prototype = supertype.prototype; //两者有什么区别
  prototype.constructor = subtype;
  subtype.prototype = prototype;
}

function SuperType(name){
  this.name = name;
}

SuperType.sayName = function(){
  alert(this.name)
}

function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
  
}

inheritPrototype(Subtype,SuperType);

例子来自于js高程。用手机打得 如有错误还望包涵

问题一 下面的两种创建对象的方式有区别吗?如果有,有什么区别

  var prototype = Object(supertype.prototype);
  var prototype = supertype.prototype; //两者有什么区别

问题二
在例子中 超类与子类的原型是否指向了同一个原型对象?寄生组合继承能否被认为是把组合式继承中本来指向超类实例的原型指针转换成直接指向超类原型,从而达到越过超类构造函数的目的?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
605 views
Welcome To Ask or Share your Answers For Others

1 Answer

问题1的两个没有区别,因为Object作为普通函数调用,内部会自动转为new调用。

问题2这个你理解可能有偏差,首先对于寄生组合式继承,依赖于原型式继承。你仔细看书会发现,你问题一中的代码与书中的不同,书中是object(上面封装过的原型式继承方法),而你写的是Object,这个我也不细说了,你重新看一下书体会一下。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...