这两种类型的继承有什么区别?
What is the difference between the two types of inheritance?
说明两种继承类型的区别?
function inherit(c,p){
var F =function(){}
F.prototype=p.prototype;
c.prototype=new F();
}
和
function inherit(c,p){
c.prototype=p.prototype;
}
如果有的话,它们的优缺点是什么?
在第二个例子中,c
和p
最终都使用了相同的对象作为它们的prototype
属性。因此,向该对象添加某些内容会影响使用new c
和new p
创建的实例,因为这些实例从函数上的prototype
属性获得其底层原型。这是一个问题,孩子不应该像那样影响父母的实例。
因此,例如,使用第二个,我们有这个问题:
function Parent() {
}
function Child() {
}
inherit(Child, Parent);
Parent.prototype.question = "Life, the Universe, and Everything";
Child.prototype.answer = 42;
var par = new Parent();
var ch = new Child();
console.log(par.question); // "Life, the Universe, and Everything"
console.log(ch.question); // "Life, the Universe, and Everything"
console.log(par.answer); // 42 <=== WRONG, Parent instances don't have the property
console.log(ch.answer); // 42
上面的代码在内存中创建了这种情况(让我们使用一个名为__proto__
的属性来表示对象的底层原型;这就是即将到来的ES6规范所做的):
对于第一个, c.prototype
是一个以p.prototype
作为其底层原型的新对象。向c.prototype
添加内容不会影响用new p
创建的实例,只会影响用new c
创建的实例。但是向p.prototype
添加东西会影响两者,如果c
意味着继承p
,这是有道理的。
function Parent() {
}
function Child() {
}
inherit(Child, Parent);
Parent.prototype.question = "Life, the Universe, and Everything";
Child.prototype.answer = 42;
var par = new Parent();
var ch = new Child();
console.log(par.question); // "Life, the Universe, and Everything"
console.log(ch.question); // "Life, the Universe, and Everything"
console.log(par.answer); // undefined, Parent instances don't have the property
console.log(ch.answer); // 42
在内存中设置:
<>之前第一个+-----------------+|函数父级|+-----------------+ +---------------+|原型 |----+----& gt; |对象 |& lt; - ++-----------------+ | +---------------+ |问题:……| |+------------+ | +---------------+ |父级参数| | |+------------+ | || __proto__ |---------+ |+------------+ ||+-----------------+ |函数子函数+-----------------+ +---------------+ ||原型 |------+--& gt; |对象 | |+-----------------+ | +---------------+ || | __proto__ |——++-----------+ | |答案:42 ||孩子ch | | +---------------++-----------+ || __proto__ |------------++-----------+之前如果有的话,它们的优缺点是什么?
第二个实际上是错误的。: -)
边注:在支持es5的环境中,第一个可以更简单:
function inherit(c,p){
c.prototype = Object.create(p.prototype);
}
旁注2:为了更彻底,第一个应该是这样的:
function inherit(c,p){
var F =function(){}
F.prototype=p.prototype;
c.prototype=new F();
c.prototype.constructor = c;
}
或:
function inherit(c,p){
c.prototype = Object.create(p.prototype);
c.prototype.constructor = c;
}
从Function
对象的prototype
属性引用的对象的constructor
属性应该指向该函数。这就是规范中设置函数的方式,尽管实际上并没有使用 constructor
属性。然而,有些人可能有使用它的代码,并期望规范的设置在那里。
相关文章:
- 全局变量和全局对象的属性之间有什么区别吗
- 在ember/handlers中使用value和valueBinding有什么区别
- 这两个关于 JavaScript 作用域链的例子有什么区别?
- Math.min()和Math.max()之间有什么区别?在Javascript中
- angularjs中的ng网格和ui网格有什么区别
- webpack开发模式和生产构建模式之间有什么区别
- 缓存!saveLocations()和addLocation()函数有什么区别
- 关于承诺/A+规范,术语“当时可”和“承诺”之间有什么区别
- 以下两者在javascript中有什么区别
- jquery.js和jquery.lite.js有什么区别
- 通过Nuget安装Angular JS和Angular JS核心有什么区别
- XMLHttpRequest对象的open()和send()方法之间有什么区别
- 这两种不同的创建对象文字的方法有什么区别
- jQuery中这两个词有什么区别
- 在Javascript语法中:和::和:::有什么区别
- getElementById 和 getElementsByClassName 有什么区别?
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- 在使用JS的Ajax调用中,ajaxComplete和beforesend之间有什么区别
- $(e.target).find和template.find('input').value之间有什么区别
- 在javascript中,在变量名之前使用var有什么区别/优势吗