一、前言
源碼閱讀過程中,發(fā)現(xiàn)以下語句
new.target.prototype
鑒于該語法為es6
所有,項目在編譯過程中,控制臺報Unexpected token: punc(.)
錯誤。按照常規(guī)處理,應(yīng)用babel-loader
即可解決此類問題。在.babelrc
的
{
"presets": [
["es2015"]
]
}
經(jīng)過實踐發(fā)現(xiàn),build
階段依舊報錯。
故采用第二套解決方案,使用es5
語法重寫es6
。
二、new.target 重寫
es5
的構(gòu)造函數(shù)前面如果不用new
調(diào)用,this
指向window
,對象的屬性就得不到值了,所以之前都要在構(gòu)造函數(shù)中通過判斷this
是否使用了new
關(guān)鍵字來確保普通的函數(shù)調(diào)用方式都能讓對象復(fù)制到屬性。
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
中,為了識別函數(shù)調(diào)用時,是否使用了new
關(guān)鍵字,引入了一個新的屬性new.target
:
-
如果函數(shù)使用了
new
,那么new.target
就是構(gòu)造函數(shù); -
如果函數(shù)沒有使用
new
,那么new.target
就是undefined
; -
es6
的類方法中,在調(diào)用時候,使用new
,new.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這個構(gòu)造函數(shù),那么上例也可以用下面這種寫法:
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關(guān)鍵字' );
7 }
8 }
9 }
10
11 // Person( 'ghostwu' ); //報錯
12 console.log( new Person( 'ghostwu' ).userName ); //ghostwu
上例,在使用new
的時候, new.target
等于Person。文章來源:http://www.zghlxwxcb.cn/news/detail-714807.html
掌握new.target
之后,接下來,我們用es5
語法改寫上文中es6
的類語法。文章來源地址http://www.zghlxwxcb.cn/news/detail-714807.html
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關(guān)鍵字' );
8 }
9 }
10
11 Object.defineProperty( Person.prototype, 'sayName', {
12 value : function(){
13 if ( typeof new.target !== 'undefined' ) {
14 throw new Error( '類里面的方法不能使用new關(guān)鍵字' );
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各版本特性詳解》
到了這里,關(guān)于JavaScript進階(二十九): 走近 es6 之 new.target的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!