Object.create setting __proto__ but not prototype

I am new to using object.create instead of the classic js method to get prototypal inheritance.

In Chrome, at least I was surprised to see the following code:

var baseObject = {
test : function(){
    console.log('Child');
}
}

var newObject = Object.create(baseObject);
newObject.test = function(){
console.log('Parent');
this.__proto__.test();
}
console.log(newObject);

newObject.test();

Produces this (simulates output in web tools):

Object {test: function, test: function} 
    test: function (){
    __proto__: Object
        test: function (){
        __proto__: Object
Parent
Child 

So you see that this is not a prototype installation, but instead only "__proto__", which I thought was discouraged in its use. You can see that in my code I can correctly inherit and call the parent object, but only using "__proto__". Using "prototype" results in an error (undefined).

? , object.create "prototype" , ( ). "__proto __"

+5
3

__proto__ , . , Object.create() .

.prototype . .prototype .

, :

function Foo() {

}

Foo .prototype, , __proto__ , .

var f = new Foo();

, f - Foo.prototype . , Object.getPrototypeOf();

Object.getPrototypeOf(f) === Foo.prototype; // true

Object.create , -. , .

var f2 = Object.create(Foo.prototype);

, , f.

Object.getPrototypeOf(f2) === Foo.prototype;            // true
Object.getPrototypeOf(f2) === Object.getPrototypeOf(f); // true

, . . __proto__ .

+5

instance __proto__ , prototype .

, __proto__ , [[Prototype]] (.. prototype ). __proto__ [[Prototype]], , .

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/proto#Description

0

Object.create() . prototype -, , [[Prototype]] , new .

__proto__is a non-standard way of accessing [[Prototype]] for a specific instance. You can also call Object.getPrototypeOf (this) for the standard way to access the prototype.

0
source

All Articles