let p={};
p.__proto__=Array.prototype;
console.log(p.slice); //ƒ slice() { [native code] }
However.. What's the difference?
let o=Object.create(null);
o.__proto__=Array.prototype;
console.log(o.slice); //undefined.
let p={};
p.__proto__=Array.prototype;
console.log(p.slice); //ƒ slice() { [native code] }
However.. What's the difference?
let o=Object.create(null);
o.__proto__=Array.prototype;
console.log(o.slice); //undefined.
__proto__ is not an ordinary property, it's an accessor, that is, some function ("setter") gets called when you assign anything to it and is responsible for setting the internal hidden [[Prototype]] property. This function is located in Object.prototype, so if your object is not connected to Object.prototype, the function is not invoked. As a result, obj.__proto__ = xxx simply creates a new property named __proto__ in your object, which doesn't do anything.
In pseudocode:
Object.prototype = {
get __proto__() {
return this.[[Prototype]]
}
set __proto__(xxx) {
this.[[Prototype]] = xxx
}
}
myObject = {} // extends Object.prototype
myObject.__proto__ = xxx // setter called!
myObject.[[Prototype]] is now xxx
myObject = Object.create(null) // extends null
myObject.__proto__ = xxx // no setter called
myObject.[[Prototype]] is still null