if(!Array.prototype.find){
Array.prototype.find=...
}
你怎么会polyfill
new.target
?当它在不支持的浏览器中使用时,它会触发语法错误。
try/catch
不起作用,因为这是语法错误。我不必使用
new.target
,我大多只是好奇。
sy
查看new.targetdocs您会看到一些可以用es5
new.target
没有
new.target
与
function Foo() {
if (!new.target) throw "Foo() must be called with new";
console.log("Foo instantiated with new");
}
Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"
function Foo() {
if (!(this instanceof Foo)) throw "Foo() must be called with new";
console.log("Foo instantiated with new");
}
Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"
在这里是使用
new.target
:
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A { constructor() { super(); } }
var a = new A(); // logs "A"
var b = new B(); // logs "B"
这是一个简单的演示:
class A {
constructor() {
// class forces constructor to be called with `new`, so
// `this` will always be set
console.log(this.constructor.name);
}
}
class B extends A { constructor() { super(); } }
var a = new A(); // logs "A"
var b = new B(); // logs "B"
Function::bind
function Abc (a) {
this.a = a;
console.log("this =", this, "; .a =", this.a, "; new.target:", new.target);
}
const A = requireNew(Abc);
const a = new A(1);
运算符一起使用的构造函数函数。使用ES3:
this
使用new.target
new
arg,因此当界限函数的来调用的。 Function.prototype.bind
操作员。
this
然后包含
this
值:
new