ES6:没有new关键字的调用类构造函数
给出一个简单的类
class Foo {
constructor(x) {
if (!(this instanceof Foo)) return new Foo(x);
this.x = x;
}
hello() {
return `hello ${this.x}`;
}}是否可以在没有new关键字的情况下调用类构造函数?
用法应该允许
(new Foo("world")).hello(); // "hello world"要么
Foo("world").hello(); // "hello world"但后者失败了
Cannot call a class as a function
长风秋雁
慕桂英3389331
相关分类