[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.
When the class is extended, it will show undefined in the parent class construcotr().
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().
相关文章推荐
- JS JSON格式操作
- LeenCode——归并问题的处理( Merge Two Sorted Lists&&Given two sorted integer arrays nums1 and nums2, merge n)
- cpp的json的用法举例
- 调用iframe中的js函数
- js操作数组方法大全
- 跟我学《JavaScript高程3》第三讲,课程笔记
- js ==与===区别(两个等号与三个等号)
- js闭包
- 全面理解Javascript闭包和闭包的几种写法及用途
- JSTL日期比较
- knockoutjs扩展与使用
- Chromimu与JS交互的测试
- kmdjs和循环依赖
- 【JavaScript】难点1-继承
- JavaScript的继承--寄生组合式继承
- js事件流
- JS参数理解(基本类型和引用类型)
- javascript将json转字符串
- chrome 插件的成果
- js中将字符串转换成json的三种方式