您的位置:首页 > Web前端 > JavaScript

[Javascript] MetaProgramming: new.target

2016-06-13 20:50 387 查看
new.target is a new “magical” value available in all functions, thoughin normal functions it will always be undefined. In any constructor,new.target always points at the constructor that new actuallydirectly invoked.

class Parent {
constructor() {
if (new.target === Parent) {
console.log( "Parent instantiated" );
}
else {
console.log( "A child instantiated" );
}
}
}

class Child extends Parent {}

var a = new Parent();
// Parent instantiated

var b = new Child(); // here new.target is undefined
// A child instantiated


When the class is extended, it will show undefined in the parent class construcotr().
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: