简体   繁体   English

这和原型javascript

[英]this and protoype javascript

Why does alert(d.prototype) return a undefined? 为什么alert(d.prototype)返回undefined? Shouldn't the prototyope be animaL? prototyope不应该是动画吗?

function Dog(){

}
function Animal(){
    this.name = "name";
}
Dog.prototype = new Animal();
var d = new Dog();

alert(d.constructor);
alert(d.prototype);
alert(d.name);

The prototype property is a property of a constructor function. prototype属性是构造函数的属性。 It's not a property of objects created with that constructor function. 它不是使用该构造函数创建的对象的属性。

Internally any object must know what its prototype is, but it's not exposed as a property with a name. 在内部,任何对象都必须知道它的原型是什么,但它不会作为具有名称的属性公开。

In some implementations it may be given a weird name. 在一些实现中,可以给出奇怪的名称。 In Firefox it's called __proto__ , but obviously you can't rely on that working in any other browser. 在Firefox中,它被称为__proto__ ,但显然你不能依赖于在任何其他浏览器中工作。

http://www.packtpub.com/article/using-prototype-property-in-javascript http://www.packtpub.com/article/using-prototype-property-in-javascript

d是一个实例,用于获取原型:

d.constructor.prototype;

FF和chrome可以为你提供__proto__的父对象,可以使用d.__proto__来访问d.__proto__

I think the drawing in http://mckoss.com/jscript/object.htm describes it (I can never remember this myself). 我认为http://mckoss.com/jscript/object.htm中的绘图描述了它(我自己永远记不起来了)。 There's a property __proto__ in Firefox that references the prototype, but other than that, going from an instance to prototype isn't done (except checking with instanceof ). Firefox中有一个属性__proto__引用原型,但除此之外,还没有完成从实例到原型的操作(除了用instanceof检查)。

原型绘图

Image is borrowed from above mentioned URL. 图像来自上述URL。

You shouldn't use Dog.prototype = new Animal(); 你不应该使用Dog.prototype = new Animal(); Try: Dog.prototype = Animal; 尝试: Dog.prototype = Animal; instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM