简体   繁体   English

ES6 super()的构造函数和原型方法

[英]ES6 super() in constructor and prototype methods

I have two questions First : I recently learnt that in ES 6 derived class, if super() is not called, the "this" is not available. 我有两个问题:首先,我最近了解到在ES 6派生类中,如果未调用super(),则“ this”不可用。 I understand why it is done this way, but I want to know conceptually, what piece of ES6 compiled code is making "this" to be unavailable. 我知道为什么要这样做,但是我想从概念上知道是什么ES6编译代码使“ this”不可用。 I mean if i want to do the same in ES5 to make "this" unavailable, how can that be done. 我的意思是,如果我想在ES5中执行相同的操作以使“此”不可用,那该怎么办。

Second: Why are we not able to instantiate ES6 prototype methods Below will not work- 第二:为什么我们无法实例化ES6原型方法,下面将不起作用-

class abc{
func(){}
}

var a = new abc()
var b = new a.func()

whereas this will work- 而这将工作-

function abc(){}

abc.prototype.func = function(){}

var a = new abc()
var b = new a.func()

WHY ? 为什么呢? For the above question as well, I want to know more about what implementation is doing this and not why they decided to provide this feature. 同样对于上述问题,我想更多地了解什么实现正在执行此操作,而不是为什么他们决定提供此功能。

I mean if i want to do the same in ES5 to make "this" unavailable, how can that be done. 我的意思是,如果我想在ES5中执行相同的操作以使“此”不可用,那该怎么办。

It's not possible. 这是不可能的。 This behaviour is new in ES6 (and really hard to transpile). 这种行为是ES6中的新功能(而且确实很难移植)。

Why are we not able to instantiate ES6 prototype methods Below will not work 为什么我们不能实例化ES6原型方法,下面的方法不起作用

Because method definitions, similar to arrow functions, are no constructors. 因为方法定义类似于箭头函数,所以不是构造函数。 Unlike function expression that can be both called and constructed. 与可以同时调用和构造的function表达式不同。

ES6 classes that use extend instantiate objects kind of in a different way from constructors. 使用ES6类的扩展实例化对象的类型与构造方法不同。

When you do new something() for constructors, there's a new object instantiated and available through this . 当为构造函数执行new something() ,将实例化一个新对象,并通过this提供this对象。 While ES6 classes that extend other classes will go up the chain and instantiate the base object, thanks to super() . 尽管扩展了其他类的ES6类将上链并实例化基础对象,这要归功于super()

In other words: 换一种说法:

class C extends B {}
class B extends A {}
class A {}
new C();

Will actually travel all the way to A and see that's not a class that uses extend and will instantiate that one (with a twist, the prototype of the object will be the prototype of the C ). 实际上将一直走到A并看到不是使用extend的类,而是将其实例化(稍作改动,对象的原型将是C的原型)。 super calls on C and B will set the this to whatever super returned. C和B的super调用会将this设置为返回的任何超级。

This is why this is never set before super() because classes that extend something will not create a new object by themselves. 这就是为什么this是从来没有设置super()因为这东西延伸类本身不能创建新的对象。

Regarding point 2, the class elements are methods (which can't be constructed), whereas functions on the prototype of constructors are normal functions that work as constructors. 关于第2点,类元素是方法(无法构造),而构造函数原型上的函数是充当构造函数的普通函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM