如何在原型链中的函数解析将对Object.prototype作为构造函数工作 [英] How function resolution in prototype chain will work for Object.prototype as constructor

查看:166
本文介绍了如何在原型链中的函数解析将对Object.prototype作为构造函数工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这篇关于Helephant.com的文章,以了解JavaScript在调用对象时如何解析原型链中的函数。

I am referring to this article on Helephant.com, to learn how Javascript resolves functions in the prototype chain when called on objects.

文章引用,


如果对象没有直接在其上设置的方法,javascript然后查找创建对象的构造函数。 Javascript检查构造函数的原型属性。

If the object doesn’t have the method set directly on it, javascript then looks for a Constructor function that created the object. Javascript checks the constructor’s prototype property for the method.

在下面的代码中,如果你选中 rufus.constructor 是全局 Object()构造函数,因此JS将直接检查全局Object(),因为 rufus.constructor Object()或根据上面的文章引用它将首先看看构造函数,然后找到原型属性等。解析函数( rufus.toString )。我很困惑这个。

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). 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; 


推荐答案


没有直接设置方法,javascript
然后查找创建对象的构造函数。
Javascript检查构造函数的方法的原型属性。

If the object doesn’t have the method set directly on it, javascript then looks for a Constructor function that created the object. Javascript checks the constructor’s prototype property for the method.

这个措辞令人困惑。 JavaScript通过 __ proto __ 使用构造函数属性查找原型链。他们提到了 Constructor ,因为通常,这是对象如何通过 Constructor.prototype 属性获得他们的原型。但这并不总是真的。你可以这样设置原型:

The wording is confusing. The JavaScript looks up the prototype chain through __proto__ and does not use constructor property. They mentioned Constructor because usually, that is how objects get their prototype - through Constructor.prototype property. 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

关于 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 

这篇关于如何在原型链中的函数解析将对Object.prototype作为构造函数工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆