es5的构造函数前面如果不用new调用,this指向window,对象的属性就得不到值了,所以以前我们都要在构造函数中通过判断this是否使用了new关键字来确保普通的函数调用方式都能让对象复制到属性

     function Person( uName ){
if ( this instanceof Person ) {
this.userName = uName;
}else {
return new Person( uName );
}
}
Person.prototype.showUserName = function(){
return this.userName;
}
console.log( Person( 'ghostwu' ).showUserName() );
console.log( new Person( 'ghostwu' ).showUserName() );

在es6中,为了识别函数调用时,是否使用了new关键字,引入了一个新的属性new.target:

1,如果函数使用了new,那么new.target就是构造函数

2,如果函数没有用new,那么new.target就是undefined

3,es6的类方法中,在调用时候,使用new,new.target指向类本身,没有使用new就是undefined

         function Person( uName ){
if( new.target !== undefined ){
this.userName = uName;
}else {
throw new Error( '必须用new实例化' );
}
}
// Person( 'ghostwu' ); //报错
console.log( new Person( 'ghostwu' ).userName ); //ghostwu

使用new之后, new.target就是Person这个构造函数,那么上例也可以用下面这种写法:

         function Person( uName ){
if ( new.target === Person ) {
this.userName = uName;
}else {
throw new Error( '必须用new实例化' );
}
} // Person( 'ghostwu' ); //报错
console.log( new Person( 'ghostwu' ).userName ); //ghostwu
         class Person{
constructor( uName ){
if ( new.target === Person ) {
this.userName = uName;
}else {
throw new Error( '必须要用new关键字' );
}
}
} // Person( 'ghostwu' ); //报错
console.log( new Person( 'ghostwu' ).userName ); //ghostwu

上例,在使用new的时候, new.target等于Person

掌握new.target之后,接下来,我们用es5语法改写上文中es6的类语法

         let Person = ( function(){
'use strict';
const Person = function( uName ){
if ( new.target !== undefined ){
this.userName = uName;
}else {
throw new Error( '必须使用new关键字' );
}
} Object.defineProperty( Person.prototype, 'sayName', {
value : function(){
if ( typeof new.target !== 'undefined' ) {
throw new Error( '类里面的方法不能使用new关键字' );
}
return this.userName;
},
enumerable : false,
writable : true,
configurable : true
} ); return Person;
})(); console.log( new Person( 'ghostwu' ).sayName() );
console.log( Person( 'ghostwu' ) ); //没有使用new,报错

最新文章

  1. 第一次写博客Poj1044
  2. Windows 下的 MarkdownPad 2 工具使用
  3. NIO提升系统性能
  4. flex布局中flex-basis|flex-grow|flex-shrink
  5. HTTP权威指南----缓存
  6. C# JSON 序列化和反序列化——JavaScriptSerializer实现
  7. 51nod1212无向图最小生成树
  8. oracle 数据库 分割字符串返回结果集函数
  9. Build A Micro Team
  10. 【译】ASP.NET MVC 5 教程 - 1:入门
  11. TortoiseGit for windows安装与配置
  12. 五年.net程序员转型Java之路
  13. 51nod 1020 逆序排列 DP
  14. bzoj千题计划242:bzoj4034: [HAOI2015]树上操作
  15. MySQL:1366 - Incorrect string value错误解决办法
  16. sqoop的安装
  17. Linux:固定 ip
  18. MongoDB的启动与停止(一)
  19. Linux-nmon系统性能监控工具的使用及报表产出
  20. 线性、逻辑回归的java实现

热门文章

  1. 关于C#中函数声明带参数的函数
  2. vue.js事件,属性,以及交互
  3. java 中Map 使用
  4. 设计模式(8) - 迭代器模式(iterator)- 实现ArrayList和linkedList的迭代器
  5. 10_Eclipse中演示Git冲突的解决
  6. 【MySQL】(4)操作数据表中的记录
  7. Intent及IntentFilter具体解释
  8. linux shell 推断文件或目录是否真的存在
  9. Django项目实践4 - Django网站管理(后台管理员)
  10. 获取手机是否root信息