[英]How function resolution in prototype chain will work for Object.prototype as constructor
I am referring to this web archive of an article originally on Helephant.com , to learn how Javascript resolves functions in the prototype chain when called on objects.我指的是最初在 Helephant.com 上的一篇文章的网络存档,以了解当在对象上调用时 Javascript 如何解析原型链中的函数。
The article quotes,文章引述,
If the object doesn't have the method set directly on it, javascript then looks for a Constructor function that created the object.
如果对象没有直接在其上设置方法,则 javascript 会查找创建该对象的构造函数。 Javascript checks the constructor's prototype property for the method.
Javascript 检查方法的构造函数的原型属性。
In the following code, if you check rufus.constructor
is the global Object()
constructor, so will JS directly check the global Object() since rufus.constructor
is Object()
or as per the article quote above it will first look at the constructor and then find the prototype property and so on.. How will JS resolve the function ( rufus.toString
).在下面的代码中,如果你检查
rufus.constructor
是全局Object()
构造函数,那么 JS 会直接检查全局 Object() 因为rufus.constructor
是Object()
或者按照上面的文章引用它会首先查看构造函数,然后找到原型属性等等.. JS 将如何解析函数( rufus.toString
)。 I am quite confused with this.我对此很困惑。
//PET CONSTRUCTOR
function Pet(name, species, hello)
{ this.name = name;
this.species = species;
this.hello = hello; }
Pet.prototype = {
sayHello : function(){
alert(this.hello);
}
}
//CAT CONSTRUCTOR
function Cat(name, hello, breed, whiskerLength)
{ this.name = name;
this.hello = hello;
this.breed = breed;
this.whiskerLength = whiskerLength;}
Cat.prototype = new Pet();
var rufus = new Cat("rufus", "miaow", "Maine Coon", 7);
rufus.toString;
If the object doesn't have the method set directly on it, javascript then looks for a Constructor function that created the object.
如果对象没有直接在其上设置方法,则 javascript 会查找创建该对象的构造函数。 Javascript checks the constructor's prototype property for the method.
Javascript 检查方法的构造函数的原型属性。
The wording is confusing.措辞令人困惑。 The JavaScript looks up the prototype chain through
__proto__
and does not use constructor
property. JavaScript 通过
__proto__
查找原型链,并且不使用constructor
属性。 They mentioned Constructor
because usually, that is how objects get their prototype - through Constructor.prototype
property.他们提到
Constructor
是因为通常,这就是对象如何获得它们的原型 - 通过Constructor.prototype
属性。 But that is not always true.但这并不总是正确的。 You can set the prototype like this:
您可以像这样设置原型:
var rufus2 = Object.create(new Pet());
Object.getPrototypeOf(rufus) === Object.getPrototypeOf(rufus2); // true
Regarding how the toString
method is resolved:关于如何解决
toString
方法:
rufus.hasOwnProperty('toString'); // false -> go up
(new Pet()) rufus.__proto__.hasOwnProperty('toString'); // false -> go up
({sayHello :...}) rufus.__proto__.__proto__.hasOwnProperty('toString'); // false -> go up
(Object.prototype) rufus.__proto__.__proto__.__proto__.hasOwnProperty('toString'); // true
function Person () { }
Person.prototype.sayName = function () { };
var bob = new Person();
console.log(bob.sayName === Person.prototype.sayName); // true
console.log(bob.constructor.prototype.sayName === Person.prototype.sayName); // true
console.log(bob.__proto__.sayName === Person.prototype.sayName); // true
console.log(bob.constructor === Person); // true
console.log(bob.__proto__ === Person.prototype); // true
Those are your relationships.这些是你的关系。
If a property / function can not be found on an object, it goes up the __proto__
chain to find what it's looking for.如果在对象上找不到属性/函数,它会沿着
__proto__
链向上查找要查找的内容。 In older browsers, __proto__
was not accessible to JS devs, but was still the object that the browser/Node used behind the scenes, to refer to the Constructor.prototype property.在旧浏览器中,JS 开发人员无法访问
__proto__
,但它仍然是浏览器/节点在幕后使用的对象,用于引用 Constructor.prototype 属性。
PS: try not to use deep hierarchy in JS. PS:尽量不要在 JS 中使用深层次。 That is a path of many tears.
那是一条泪流满面的路。 Deep hierarchies cause pain in other class-based languages, but are misery in JS, where often a function would suffice.
深层次结构在其他基于类的语言中会导致痛苦,但在 JS 中却是痛苦的,通常一个函数就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.