据说所有的Javascript对象都有一个原型属性,但是如果foo是一个函数,我只会看到foo.prototype? [英] It is said that all Javascript objects have a prototype property, but I only see foo.prototype if foo is a function?

查看:33
本文介绍了据说所有的Javascript对象都有一个原型属性,但是如果foo是一个函数,我只会看到foo.prototype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常说每个Javascript对象都有一个 prototype 属性,但我发现 foo.prototype 仅在 foo 时才有一个值是一个功能.

It is often said that every Javascript object has a prototype property, but I find that foo.prototype has a value only if foo is a function.

在Chrome和Firefox上, obj .__ proto __ 有一个值-这是所说的 prototype 属性吗?但是在IE 9上,它将无法正常工作(有某种方法可以吗?),我认为通过 prototype 属性,这意味着 obj.prototype 应该可以工作吗?

On Chrome and Firefox, obj.__proto__ has a value -- is this the said prototype property? But on IE 9, it won't work (is there some way that can?), and I thought by prototype property, that means obj.prototype should work?

我知道 Object.getPrototypeOf(obj)似乎显示了此 prototype 属性,但是为什么需要一种特殊的方法来获取它?为什么不像 person.name 那样获得 person 对象的 name 属性?

I understand that Object.getPrototypeOf(obj) seems to show this prototype property, but why need a special method to get it? Why not just like person.name, which is to get the name property of the person object?

更新:顺便说一句, obj.constructor.prototype 有时是那个原型,但有时不是那样,如下所示在没有构造函数的情况下通过原型继承完成的代码:(此方法在Harmes和Diaz撰写的Pro Javascript Design Patterns一书中,Apress 2008年发表,第46页)

Update: by the way, obj.constructor.prototype seems to sometimes be that prototype, but sometimes not, as in the following code done with Prototypal inheritance with no constructor: (this method is in the Pro Javascript Design Patterns book by Harmes and Diaz by Apress 2008, p. 46)

var Person = {
    name: 'default value',
    getName: function() {
        return this.name;
    }
}

var reader = clone(Person);
console.log(reader.getName());
reader.name = "Ang Lee";
console.log(reader.getName());

function clone(obj) {
    function F() {};
    F.prototype = obj;
    return new F;
}

console.log("the prototype of reader is", Object.getPrototypeOf(reader));

console.log(Object.getPrototypeOf(reader) === reader.constructor.prototype);
console.log(Object.getPrototypeOf(reader) == reader.constructor.prototype);

console.log(Object.getPrototypeOf(reader) === reader.__proto__);
console.log(Object.getPrototypeOf(reader) == reader.__proto__);

最后4行的结果将显示false,false,true,true.

the result will show false, false, true, true for the last 4 lines.

推荐答案

每个JavaScript对象都有一个内部的原型"属性,通常称为[[prototype]],该属性指向它直接继承的对象.这是通过非标准的 __ proto __ 属性在FF和Chrome中公开的. Object.getPrototypeOf 是此内部属性的获取器.

Every JavaScript object has an internal "prototype" property, often called [[prototype]], which points to the object from which it directly inherits. This is exposed in FF and Chrome by the non-standard __proto__ property. Object.getPrototypeOf is a getter for this internal property.

每个JavaScript函数[object]都有一个属性 prototype ,该属性用一个[几乎]空的对象初始化.当您通过将其作为构造函数调用来创建此函数的新实例时,该新对象的[[prototype]]将指向构造函数的 prototype 对象.

Every JavaScript function [object] has a property prototype, which is initialized with an [nearly] empty object. When you create a new instance of this function by calling it as a constructor, the [[prototype]] of that new object will point to the constructor's prototype object.

如果获得函数的[[prototype]](每个函数都是一个对象,因此有一个),它将导致 Function.prototype 对象,函数从该对象继承其方法(例如绑定,调用,应用等).另请参见为什么函数原型反复链接?.

If you get the [[prototype]] of a function (every function is an object, so it has one), it will result in the Function.prototype object from which functions inherit their methods (like bind, call, apply etc). See also Why functions prototype is chained repeatedly? on that.

这篇关于据说所有的Javascript对象都有一个原型属性,但是如果foo是一个函数,我只会看到foo.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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