6To5编译器 - 使用__proto__进行继承 [英] 6To5 Compiler - use __proto__ for inheritance

查看:182
本文介绍了6To5编译器 - 使用__proto__进行继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为6to5的输出,我有以下代码:

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);

任何人都可以描述用于更新的内容 __ proto __ 属性?当我尝试 - 它没有什么有用的

Can anybody describe what for used updating __proto__ property? As i try - it doesn't nothing useful

P.S。 X-4545 X-4545 X-45 X-454545 X-454545 X-454545 X- 20045 X-454545 X-454545 X- 20045 X- 20045 X-对象,但在代码中用于设置函数

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

另外,在 github yuchi 说:


注意扩展后如何添加静态属性。

Notice how ‘static’ properties are added after extension.

不推荐使用它,因为它不会让解释器事先知道
,该对象(在这种情况下,函数)具有与标准对象不同的
原型链。每个JS解释器实现
它,这是因为在
Object.setPrototypeOf上仍然没有完美的一致性,实际上由于性能原因,IMO。

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.

看起来像6to5想要尽可能兼容ES6
语义,所以设置Child。 proto 到Parent是一个可以接受的
权衡。

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.

详细的讨论可以通过链接找到: https://github.com/babel/babel/issues/87#issuecomment-60139066

The detailed discussion you can find by the link: https://github.com/babel/babel/issues/87#issuecomment-60139066

另外,你可以看来自 basarat 关于突变[[原型]]和性能的好消息:
为什么突变[[原型]]的对象不好表演?

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?

这篇关于6To5编译器 - 使用__proto__进行继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆