Javascript 返回 ES6 中类以外的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27980996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return a value other than the class in ES6
提问by MikaAK
Recently I've been testing out classes with ES6, I've noticed that when creating a class you cannot specify the value given by the constructor.
最近我一直在用 ES6 测试类,我注意到在创建类时你不能指定构造函数给出的值。
Previously in ES5 this was possible.
以前在 ES5 中这是可能的。
In both cases I would instantiate the class with new MyClassThe reason I want to do this is so I can return a subset of the current class with only functions on it.
在这两种情况下,我都会使用new MyClass我想这样做的原因来实例化类,以便我可以返回当前类的子集,其中只有函数。
ES5 - returns My class was init with: Blah
ES5 - 返回 My class was init with: Blah
var MyClass = function() {
this.initVar = 'Blah'
return 'My Class was init with: ' + this.initVar
}
ES6 - returns {}
ES6 - 返回 {}
class Bob {
constructor() {
return 'hello'
}
}
回答by RobG
According to the Classarticlefrom the TC39 web site, the ES6 class syntax has an implicit constructor function that is called if no such function is provided in the class definition.
根据TC39 网站上的Class文章,ES6 类语法有一个隐式构造函数,如果类定义中没有提供此类函数,则会调用该函数。
This can be overridden by providing your own constructor and returning whatever object you want, e.g.:
这可以通过提供您自己的构造函数并返回您想要的任何对象来覆盖,例如:
class Bob {
constructor(name) {
return {hi: name}; // returns an object other than *this*.
}
}
In action:
在行动:
var bob = new Bob('bob');
console.log(bob.hi); // 'bob'
To extend the class, you can do:
要扩展类,您可以执行以下操作:
class Bill extends Bob {
}
However extendsalso has an implicit constructor, so it will return a new instance of Billthat inherits from Bob.prototype. Since hiwas defined in the constructor and not inherited, you get:
然而extends也有一个隐式构造函数,因此它将返回一个从Bob.prototype继承的Bill的新实例。由于hi是在构造函数中定义的而不是继承的,因此您会得到:
var bill = new Bill('bill');
console.log(bill.hi); // undefined
To initialise Bill the same as Bob, provide a constructor that calls super. This call also changes the thisobject of Billto the value returned by super, e.g.
要像Bob一样初始化 Bill ,请提供一个调用super的构造函数。此调用还将Bill的this对象更改为super返回的值,例如
class Bill extends Bob {
constructor(name) {
super(name);
}
}
Now:
现在:
var bill = new Bill('bill');
console.log(bill.hi); // bill
Also worth noting that the body of a classDeclarationis always strict mode code.
还值得注意的是,classDeclaration的主体始终是严格模式代码。
As a runnable snippet:
作为一个可运行的片段:
class Bob {
constructor(name) {
return {hi: name}; // returns an object other than *this*.
}
}
var bob = new Bob('bob');
console.log(bob.hi); // 'bob'
class Bill extends Bob {
constructor(name) {
super(name);
}
}
var bill = new Bill('bill');
console.log(bill.hi); // bill
回答by Naeem Shaikh
Compare these fiddles: es5and es6
What you say was possible in es5 is still possible in es6, there is a small thing that if you use new keyword, then a new object for that class is created and if you dont ue new then the function is executed.
你说在 es5 中可能的在 es6 中仍然可能,有一点是,如果你使用 new 关键字,那么会为该类创建一个新对象,如果你不使用 new 则执行该函数。
So when you say,
var x= Bob();in both es5 and es6, you execute the constructor function, not create a new object, thus it returns something.And when you say,
var x= new Bob();, you get a new object, initialised by the constructor function.
所以当你说,
var x= Bob();在 es5 和 es6 中,你执行构造函数,而不是创建一个新对象,因此它返回一些东西。当你说, 时
var x= new Bob();,你会得到一个由构造函数初始化的新对象。
This is applicable to both es5 and es6, as es6 classes does nothing new , but are introduced just for the sake of syntax.
这对 es5 和 es6 都适用,因为 es6 类没有任何新功能,只是为了语法而引入。
Edit: In case of extending classes: You can not just extend a class in es6 and expect that the super constructor is called, you need to call it explicitly inside the child class constructor.. see the code:
编辑:在扩展类的情况下:您不能只在 es6 中扩展一个类并期望调用超级构造函数,您需要在子类构造函数中显式调用它..见代码:
class Bob {
constructor() {
return {hi: 'bob'}
}
}
class Bill extends Bob {
constructor() {
return super();// here you should retun the called super constructer
}
}
var x= Bob();
console.log(Bob);
x= new Bill();
console.log(x.hi);
回答by Pawe?
The ES6actually does not return {}but the class (constructor)object in that case. The class constructor can return objects, but not primitive values. So instead of [String] "hello"it returns [Object] Bob. Any value can be returned this way:
在这种情况下,ES6实际上不返回,{}而是返回类(构造函数)对象。类构造函数可以返回对象,但不能返回原始值。因此,"hello"它返回的不是 [String]而是 [Object] Bob。任何值都可以通过这种方式返回:
class Bob {
constructor() {
return ()=>'hello';
}
}
const bob = new Bob()();
The returned [Function], as it is an object, can be returned and immediately fired to return some primitive value, eg. [String] "hello".
返回的 [Function],因为它是一个对象,可以返回并立即触发以返回一些原始值,例如。[字符串] "hello"。

