Javascript模擬實(shí)現(xiàn)new原理解析
new是JS中的一個(gè)關(guān)鍵字,用來將構(gòu)造函數(shù)實(shí)例化的一個(gè)運(yùn)算符。例子:
function Animal(name) {
this.name = name;
}
Animal.prototype.sayName = function() {
console.log("I'm " + this.name);
}
var cat = new Animal('Tom');
console.log(cat.name); // Tom
console.log(cat.__proto__ === Animal.prototype); // true
cat.sayName(); // I'm Tom
從上面的例子可以得出兩點(diǎn)結(jié)論:
- new操作符實(shí)例化了一個(gè)對(duì)象;
- 這個(gè)對(duì)象可以訪問構(gòu)造函數(shù)的屬性;
- 這個(gè)對(duì)象可以訪問構(gòu)造函數(shù)原型上的屬性;
- 對(duì)象的**__proto__**屬性指向了構(gòu)造函數(shù)的原型;
由于new是關(guān)鍵字,我們只能去聲明一個(gè)函數(shù)去實(shí)現(xiàn)new的功能,首先實(shí)現(xiàn)上面的三個(gè)特性,第一版代碼如下:
附:對(duì)原型原型鏈不熟悉的可以先看理解Javascript的原型和原型鏈。
// construct: 構(gòu)造函數(shù)
function newFunction() {
var res = {};
// 排除第一個(gè)構(gòu)造函數(shù)參數(shù)
var construct = Array.prototype.shift.call(arguments);
res.__proto__ = construct.prototype;
// 使用apply執(zhí)行構(gòu)造函數(shù),將構(gòu)造函數(shù)的屬性掛載在res上面
construct.apply(res, arguments);
return res;
}
我們測試下:
function newFunction() {
var res = {};
var construct = Array.prototype.shift.call(arguments);
res.__proto__ = construct.prototype;
construct.apply(res, arguments);
return res;
}
function Animal(name) {
this.name = name;
}
Animal.prototype.sayName = function() {
console.log("I'm " + this.name);
}
var cat = newFunction(Animal, 'Tom');
console.log(cat.name); // Tom
console.log(cat.__proto__ === Animal.prototype); // true
cat.sayName(); // I'm Tom
一切正常。new的特性實(shí)現(xiàn)已經(jīng)80%,但new還有一個(gè)特性:
function Animal(name) {
this.name = name;
return {
prop: 'test'
};
}
var cat = new Animal('Tom');
console.log(cat.prop); // test
console.log(cat.name); // undefined
console.log(cat.__proto__ === Object.prototype); // true
console.log(cat.__proto__ === Animal.prototype); // false
如上,如果構(gòu)造函數(shù)return了一個(gè)對(duì)象,那么new操作后返回的是構(gòu)造函數(shù)return的對(duì)象。讓我們來實(shí)現(xiàn)下這個(gè)特性,最終版代碼如下:
// construct: 構(gòu)造函數(shù)
function newFunction() {
var res = {};
// 排除第一個(gè)構(gòu)造函數(shù)參數(shù)
var construct = Array.prototype.shift.call(arguments);
res.__proto__ = construct.prototype;
// 使用apply執(zhí)行構(gòu)造函數(shù),將構(gòu)造函數(shù)的屬性掛載在res上面
var conRes = construct.apply(res, arguments);
// 判斷返回類型
return conRes instanceof Object ? conRes : res;
}
測試下:
function Animal(name) {
this.name = name;
return {
prop: 'test'
};
}
var cat = newFunction(Animal, 'Tom');
console.log(cat.prop); // test
console.log(cat.name); // undefined
console.log(cat.__proto__ === Object.prototype); // true
console.log(cat.__proto__ === Animal.prototype); // false
以上代碼就是我們最終對(duì)new操作符的模擬實(shí)現(xiàn)。我們?cè)賮砜聪鹿俜綄?duì)new的解釋
引用MDN對(duì)new運(yùn)算符的定義:
new 運(yùn)算符創(chuàng)建一個(gè)用戶定義的對(duì)象類型的實(shí)例或具有構(gòu)造函數(shù)的內(nèi)置對(duì)象的實(shí)例。
new操作符會(huì)干下面這些事:
- 創(chuàng)建一個(gè)空的簡單JavaScript對(duì)象(即{});
- 鏈接該對(duì)象(即設(shè)置該對(duì)象的構(gòu)造函數(shù))到另一個(gè)對(duì)象 ;
- 將步驟1新創(chuàng)建的對(duì)象作為this的上下文 ;
- 如果該函數(shù)沒有返回對(duì)象,則返回this。
4條都已經(jīng)實(shí)現(xiàn)。還有一個(gè)更好的實(shí)現(xiàn),就是通過Object.create去創(chuàng)建一個(gè)空的對(duì)象:
// construct: 構(gòu)造函數(shù)
function newFunction() {
// 通過Object.create創(chuàng)建一個(gè)空對(duì)象;
var res = Object.create(null);
// 排除第一個(gè)構(gòu)造函數(shù)參數(shù)
var construct = Array.prototype.shift.call(arguments);
res.__proto__ = construct.prototype;
// 使用apply執(zhí)行構(gòu)造函數(shù),將構(gòu)造函數(shù)的屬性掛載在res上面
var conRes = construct.apply(res, arguments);
// 判斷返回類型
return conRes instanceof Object ? conRes : res;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序?qū)崿F(xiàn)星星評(píng)價(jià)效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)星星評(píng)價(jià)效果,支持多個(gè)條目評(píng)價(jià),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
基于JavaScript中字符串的match與replace方法(詳解)
下面小編就為大家介紹一下JavaScript中的字符串的match與replace方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
JS把內(nèi)容動(dòng)態(tài)插入到DIV的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫S把內(nèi)容動(dòng)態(tài)插入到DIV的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
點(diǎn)擊提交按鈕后按鈕變灰色不可用狀態(tài)的三種方法
當(dāng)點(diǎn)擊提交后,提交按鈕變灰色不可用,這樣可有效防止重復(fù)提交,本代碼就是實(shí)現(xiàn)這樣一個(gè)功能2013-09-09

