Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ecmascript 6 为什么super()返回es6中新创建的对象?_Ecmascript 6 - Fatal编程技术网

Ecmascript 6 为什么super()返回es6中新创建的对象?

Ecmascript 6 为什么super()返回es6中新创建的对象?,ecmascript-6,Ecmascript 6,据我所知 super([arguments])关键字调用父构造函数 具有以下基本类别: class X{ constructor(){ return 'x'; // doesn't matter when calling super() } } class Z extends X{ constructor(){ console.log('This will return the newly created object invoked by

据我所知

super([arguments])关键字调用父构造函数

具有以下基本类别:

class X{
    constructor(){
       return 'x'; // doesn't matter when calling super()
    }
}
class Z extends X{
    constructor(){
        console.log('This will return the newly created object invoked by new keyword', super())
    }
}
new Z; // Z {}

为什么
super()
返回全新的Z对象?

您不能从构造函数返回基元类型

X类{
构造函数(){
//设置正确的原型
返回Object.setPrototypeOf(新字符串(“x”)、new.target.prototype);
//或
//返回新字符串('x')只是为了表明可以返回任何对象
}
}
类Z扩展了X{
构造函数(){
log('这将返回由new关键字'super()调用的新创建的对象)
}
}

console.log(新Z);//Z{}
不能从构造函数返回基元类型

X类{
构造函数(){
//设置正确的原型
返回Object.setPrototypeOf(新字符串(“x”)、new.target.prototype);
//或
//返回新字符串('x')只是为了表明可以返回任何对象
}
}
类Z扩展了X{
构造函数(){
log('这将返回由new关键字'super()调用的新创建的对象)
}
}

console.log(新Z);//Z{}
你还期待什么?@Bergi可能没有定义。。。它背后的原因是什么?是的,
undefined
可能也起作用,但它只是返回刚刚初始化的
这个
值。你还期望什么?@Bergi可能未定义。。。它背后的原因是什么?是的,
undefined
可能也起作用,但它只是返回刚刚初始化的
这个
值。你应该
返回Object.setPrototypeOf(new String(“x”),new.target.prototype)
好的,我明白了,但我的主要问题是为什么?这种行为的原因是什么?即使父构造函数不返回任何内容,它仍将返回我的新object@BarbuBarbu这就是如何在js中实现
new
关键字
var x=new-Ctor
大致相当于
tmp=Object.create(Ctor.prototype);tmp2=应用系数(tmp);var x=tmp2的类型==='对象'?tmp2:tmp
它检查构造函数返回的内容。如果它是对象类型的东西,则将其用作结果。我知道,但是如果您在控制台中运行我的代码,您将看到Z{}将返回两次,一次是通过new关键字返回,第二次是通过super()返回call@BarbuBarbu好的,
super()
内部构造函数调用父构造函数。如果后者返回,则
super
调用也可以。你可以检查一下以便更好地理解。注意
\u possibleConstructorReturn
您应该
返回Object.setPrototypeOf(新字符串(“x”),new.target.prototype)
好的,我明白了,但我的主要问题是为什么?这种行为的原因是什么?即使父构造函数不返回任何内容,它仍将返回我的新object@BarbuBarbu这就是如何在js中实现
new
关键字
var x=new-Ctor
大致相当于
tmp=Object.create(Ctor.prototype);tmp2=应用系数(tmp);var x=tmp2的类型==='对象'?tmp2:tmp
它检查构造函数返回的内容。如果它是对象类型的东西,则将其用作结果。我知道,但是如果您在控制台中运行我的代码,您将看到Z{}将返回两次,一次是通过new关键字返回,第二次是通过super()返回call@BarbuBarbu好的,
super()
内部构造函数调用父构造函数。如果后者返回,则
super
调用也可以。你可以检查一下以便更好地理解。注意
\u possibleConstructorReturn