简体   繁体   English

JavaScript 的原型继承是如何工作的?

[英]How does JavaScript's prototypal Inheritance work?

I want to know how JavaScript's Prototypal inheritance works.When we are creating an object with the new keyword the object's __proto__ is set to Constructor_Function.prototype .我想知道 JavaScript 的原型继承是如何工作的。当我们使用 new 关键字创建对象时,对象的__proto__设置为Constructor_Function.prototype

But I don't understand why I am getting this Output.但我不明白为什么我会得到这个输出。

My code :我的代码:

function SimpleFunction(){}

let obj = new SimpleFunction();

console.dir(obj);

Output :输出 :

SimpleFunction {}
    [[Prototype]]: Object
        constructor: ƒ SimpleFunction()
        [[Prototype]]: Object
            constructor: ƒ Object()
            hasOwnProperty: ƒ hasOwnProperty()
            isPrototypeOf: ƒ isPrototypeOf()
            propertyIsEnumerable: ƒ propertyIsEnumerable()
            toLocaleString: ƒ toLocaleString()
            toString: ƒ toString()
            valueOf: ƒ valueOf()
            __defineGetter__: ƒ __defineGetter__()
            __defineSetter__: ƒ __defineSetter__()
            __lookupGetter__: ƒ __lookupGetter__()
            __lookupSetter__: ƒ __lookupSetter__()
            __proto__: Object
                constructor: ƒ SimpleFunction()
                [[Prototype]]: Object
                constructor: ƒ Object()
                hasOwnProperty: ƒ hasOwnProperty()
                isPrototypeOf: ƒ isPrototypeOf()
                propertyIsEnumerable: ƒ propertyIsEnumerable()
                toLocaleString: ƒ toLocaleString()
                toString: ƒ toString()
                valueOf: ƒ valueOf()
                __defineGetter__: ƒ __defineGetter__()
                __defineSetter__: ƒ __defineSetter__()
                __lookupGetter__: ƒ __lookupGetter__()
                __lookupSetter__: ƒ __lookupSetter__()
                __proto__: Object
                    constructor: ƒ Object()
                    hasOwnProperty: ƒ hasOwnProperty()
                    isPrototypeOf: ƒ isPrototypeOf()
                    propertyIsEnumerable: ƒ propertyIsEnumerable()
                    toLocaleString: ƒ toLocaleString()
                    toString: ƒ toString()
                    valueOf: ƒ valueOf()
                    __defineGetter__: ƒ __defineGetter__()
                    __defineSetter__: ƒ __defineSetter__()
                    __lookupGetter__: ƒ __lookupGetter__()
                    __lookupSetter__: ƒ __lookupSetter__()
                    __proto__: null
                    get __proto__: ƒ __proto__()
                    set __proto__: ƒ __proto__()
                get __proto__: ƒ __proto__()
                set __proto__: ƒ __proto__()
            get __proto__: ƒ __proto__()
            set __proto__: ƒ __proto__()

And When I tried this :当我尝试这个时:

obj.__proto__.__proto__.__proto__;

I got :我有 :

null

So I don't understand why there is more than three prototype objects in the Output.所以我不明白为什么输出中有超过三个原型对象。

So I don't understand why there is more than three prototype objects in the Output.所以我不明白为什么输出中有超过三个原型对象。

It's because the SimpleFunction() constructor function is the derivative source object ( functions are objects with prototypes too ).这是因为SimpleFunction()构造函数是派生源对象( 函数也是具有原型的对象)。 Since SimpleFunction() is user defined, it must inherit from the Object object just like every other object does.由于SimpleFunction()是用户定义的,因此它必须像所有其他对象一样从 Object 对象继承。 Therefore you have the function object and its prototype the Object object, and then you have obj that is derived from the constructor with it's own Object object prototype.因此,您拥有函数对象及其原型 Object 对象,然后您拥有从构造函数派生的obj及其自己的 Object 对象原型。

Reference 参考

If interested, here is the spec that describes this process如果有兴趣,这里是描述此过程规范

And no, [[Prototype]] and __proto__ are not the same thing.不,[[Prototype]] 和__proto__不是一回事。 If you look at the log, you will see setter and getters named __proto__ .如果您查看日志,您将看到名为__proto__ setter 和 getter。 So __proto__ is actually an accessor property for [[Prototype]] while [[Prototype]] reflects the actual object that is the prototype.所以__proto__实际上是存取器属性[[原型]],而[[原型]]反映了实际的对象,它原型。

Reference 参考

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

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