时间: 2020-11-23|63次围观|0 条评论

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().

 

转载于:https://www.cnblogs.com/Answer1215/p/5582031.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/97760788

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《[Javascript] MetaProgramming: new.target
   

还没有人抢沙发呢~