Javascript 如果我可以';你不能访问它的输出吗?
给出以下示例:Javascript 如果我可以';你不能访问它的输出吗?,javascript,ecmascript-6,Javascript,Ecmascript 6,给出以下示例: X类扩展(()=>{ 返回“垃圾邮件” }) { 构造函数(){ super()//无法访问 this.msg=X()//无法访问 } } 设x=new x() console.log(x.msg)编译或执行的内容并不意味着它有效或符合规范。因此,您始终需要检查规范中哪些是有效的,哪些是无效的。您的问题的相关部分可在以下内容中找到: 如果IsConstructor(超类)为false,则抛出TypeError异常。并且箭头函数不是构造函数 因此,在评估类定义时,引擎需要根据规范
X类扩展(()=>{
返回“垃圾邮件”
}) {
构造函数(){
super()//无法访问
this.msg=X()//无法访问
}
}
设x=new x()
console.log(x.msg)
编译或执行的内容并不意味着它有效或符合规范。因此,您始终需要检查规范中哪些是有效的,哪些是无效的。您的问题的相关部分可在以下内容中找到:
如果IsConstructor(超类)为false,则抛出TypeError异常。并且箭头函数不是构造函数
因此,在评估类定义时,引擎需要根据规范抛出TypeError
错误
而Chrome(76)实际上在计算x=new x()之前抛出错误:
未捕获类型错误:类扩展值()=>{
返回“垃圾邮件”
}不是构造函数或null
是具有[[Construct]]内部方法的函数对象
ClassHeritage
定义如下:
天哪,我的眼睛受伤了DIt是模糊的,因为它们是相同的东西。类只是现有函数机制之上的一种不同语法,它有一些关于如何使用它的规则(例如,如果没有
new
,就不能调用实例类)。在firefox 70.0b4(64位)(在linux上)中,你不能TypeError:()=>{return“spam”}不是构造函数
当使用babel传输时,你代码中唯一的错误就是this.msg=X()
出于显而易见的原因为什么一开始就可以扩展箭头函数
事实并非如此-只有在使用es2015 loose进行传输时才能克服这一棘手问题
[...]
5. If ClassHeritageopt is not present, then
a. Let protoParent be the intrinsic object %ObjectPrototype%.
b. Let constructorParent be the intrinsic object %FunctionPrototype%.
6. Else,
a. Set the running execution context's LexicalEnvironment to classScope.
b. Let superclassRef be the result of evaluating ClassHeritage.
c. Set the running execution context's LexicalEnvironment to lex.
d. Let superclass be ? GetValue(superclassRef).
e. If superclass is null, then
i. Let protoParent be null.
ii. Let constructorParent be the intrinsic object %FunctionPrototype%.
f. Else if IsConstructor(superclass) is false, throw a TypeError exception.
[...]
ClassHeritage [Yield, Await]:
extends LeftHandSideExpression[?Yield, ?Await]