How to use the javascript object.getprototype function?

  node.js, question

References sample test mentioned aboveObject.getPrototypeOfThe results of the function are different. They are both in node environment and browser environment.

var proto = { y: 2 };

var obj = { x: 10 };
Object.getPrototypeOf(obj, proto);

proto.y = 20;
proto.z = 40;

if (console && console.log) {
    console.log(obj.x === 10);  // Returns true
    console.log(obj.y === 20);  // Returns true
    console.log(obj.z === 40);  // Returns true

As shown in the above example, the actual printing result is


Seek a solution ..

You look at the code carefully, the third sentence code
Object.getPrototypeOf(obj, proto);

According to the documentation, it should be set upobjThe prototype of isprotoIt’s obviously wrong to write here
Object.setPrototypeOf(obj, proto);

In this way, all three outputs are true.

At the same time,Object.getPrototypeOf(obj)This method takes only one parameter, which is to get the prototype object.