As the output of 6to5 I've got the following code:
var _inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
// question is about next row
if (superClass) subClass.__proto__ = superClass;
};
_inherits(Controller, CoreController);
Can anybody describe what for used updating __proto__ property? As i try - it doesn't nothing useful
P.S. as documentation says that proto should be object, but in code is used for setting function
解决方案
It's used for 'static" properties inheritance. Please see the code example below:
class Parent {
myMethod() {}
}
console.log(Parent.prototype.myMethod); // [Function]
console.log(Child.prototype.hasOwnProperty('myMethod')); // true
console.log(Parent.myMethod); // undefined
class Child extends Parent {}
console.log(Child.prototype.myMethod); // Function
console.log(Child.prototype.hasOwnProperty('myMethod')); // false
console.log(Child.myMethod); // undefined
Parent.myStaticProp = 42;
console.log(Parent.prototype.myStaticProp); // undefined
console.log(Parent.myStaticProp); // 42
console.log(Parent.hasOwnProperty('myStaticProp')); // true
console.log(Child.prototype.myStaticProp); // undefined
console.log(Child.myStaticProp); // 42
console.log(Child.hasOwnProperty('myStaticProp')); // false
Also, in the similar question on github, yuchi said:
Notice how ‘static’ properties are added after extension.
It’s deprecated because it doesn’t give the interpreter a way to know
beforehand that the object (function, in this case) has a different
prototype chain than the standard one. Every JS interpreter implements
it, and that’s because there’s still no perfect agreement on
Object.setPrototypeOf, in fact for performance reasons IMO.
And looks like that 6to5 wants to be as compatible as possible to ES6
semantics, so setting Child.proto to Parent is an acceptable
tradeoff.
Also, you can look at the good question from basarat about mutating [[Prototype]] and performance by the link:
Why is mutating the [[prototype]] of an object bad for performance?