Why does Babel use setPrototypeOf for inheritance when it already does Object.create(superClass.prototype)?

后端 未结 1 1150
天涯浪人
天涯浪人 2021-01-12 10:26

Posting the following code into the Babel REPL

class Test {

}

class Test2 extends Test {

}

you get this inherits function

相关标签:
1条回答
  • 2021-01-12 10:54

    The setPrototypeOf does set the [[prototype]] of subClass from its original value Function.prototype to superClass, to let it inherit static properties from it.

    Object.create cannot be used here (like it is for the .prototype object), as it does not allow to create functions. The constructor of a class has to be a function though, obviously; and the only way to do that is to create functions using standard expressions/declarations and then change its prototype afterwards.

    0 讨论(0)
提交回复
热议问题