JavaScript进阶(二十九): 走近 es6 之 new.target

编程入门 行业动态 更新时间:2024-10-10 15:23:26

JavaScript<a href=https://www.elefans.com/category/jswz/34/1769503.html style=进阶(二十九): 走近 es6 之 new.target"/>

JavaScript进阶(二十九): 走近 es6 之 new.target

文章目录

    • 一、前言
    • 二、new.target 重写
    • 三、拓展阅读

一、前言

源码阅读过程中,发现以下语句

new.target.prototype

鉴于该语法为es6所有,项目在编译过程中,控制台报Unexpected token: punc(.)错误。按照常规处理,应用babel-loader即可解决此类问题。在.babelrc

{"presets": [["es2015"]]
}

经过实践发现,build阶段依旧报错。

故采用第二套解决方案,使用es5语法重写es6

二、new.target 重写

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

 1     function Person( uName ){2         if ( this instanceof Person ) {3             this.userName = uName;4         }else {5             return new Person( uName );6         }7     }8     Person.prototype.showUserName = function(){9         return this.userName;
10     }
11     console.log( Person( 'ghostwu' ).showUserName() );
12     console.log( new Person( 'ghostwu' ).showUserName() );

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

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

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

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

1         function Person( uName ){
2             if( new.target !== undefined ){
3                 this.userName = uName;
4             }else {
5                 throw new Error( '必须用new实例化' );
6             }
7         }
8         // Person( 'ghostwu' ); //报错
9         console.log( new Person( 'ghostwu' ).userName ); //ghostwu

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

 1         function Person( uName ){2             if ( new.target === Person ) {3                 this.userName = uName;4             }else {5                 throw new Error( '必须用new实例化' );6             }7         }8         9         // Person( 'ghostwu' ); //报错
10         console.log( new Person( 'ghostwu' ).userName ); //ghostwu
 1         class Person{2             constructor( uName ){3                 if ( new.target === Person ) {4                     this.userName = uName;5                 }else {6                     throw new Error( '必须要用new关键字' );7                 }8             }            9         }
10 
11         // Person( 'ghostwu' ); //报错
12         console.log( new Person( 'ghostwu' ).userName ); //ghostwu

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

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

 1         let Person = ( function(){2             'use strict';3             const Person = function( uName ){4                 if ( new.target !== undefined ){5                     this.userName = uName;6                 }else {7                     throw new Error( '必须使用new关键字' );8                 }9             }
10 
11             Object.defineProperty( Person.prototype, 'sayName', {
12                 value : function(){
13                     if ( typeof new.target !== 'undefined' ) {
14                         throw new Error( '类里面的方法不能使用new关键字' );
15                     }
16                     return this.userName;
17                 },
18                 enumerable : false,
19                 writable : true,
20                 configurable : true
21             } );
22 
23             return Person;
24         })();
25 
26         console.log( new Person( 'ghostwu' ).sayName() );
27         console.log( Person( 'ghostwu' ) ); //没有使用new,报错

三、拓展阅读

  • 《JavaScript进阶(二十六):ES各版本特性详解》

更多推荐

JavaScript进阶(二十九): 走近 es6 之 new.target

本文发布于:2023-12-03 10:45:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1654481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进阶   二十九   JavaScript   target

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!