码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] MetaProgramming: new.target

时间:2016-06-13 21:47:31      阅读:123      评论:0      收藏: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().

 

[Javascript] MetaProgramming: new.target

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5582031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!