色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Javascript模擬實現(xiàn)new原理解析

瀏覽:102日期:2023-11-09 11:21:09

new是JS中的一個關(guān)鍵字,用來將構(gòu)造函數(shù)實例化的一個運算符。例子:

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); // Tomconsole.log(cat.__proto__ === Animal.prototype); // truecat.sayName(); // I’m Tom

從上面的例子可以得出兩點結(jié)論:

new操作符實例化了一個對象; 這個對象可以訪問構(gòu)造函數(shù)的屬性; 這個對象可以訪問構(gòu)造函數(shù)原型上的屬性; 對象的**__proto__**屬性指向了構(gòu)造函數(shù)的原型;

由于new是關(guān)鍵字,我們只能去聲明一個函數(shù)去實現(xiàn)new的功能,首先實現(xiàn)上面的三個特性,第一版代碼如下:

附:對原型原型鏈不熟悉的可以先看理解Javascript的原型和原型鏈。

// construct: 構(gòu)造函數(shù)function newFunction() { var res = {}; // 排除第一個構(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); // Tomconsole.log(cat.__proto__ === Animal.prototype); // truecat.sayName(); // I’m Tom

一切正常。new的特性實現(xiàn)已經(jīng)80%,但new還有一個特性:

function Animal(name) { this.name = name; return { prop: ’test’ };}var cat = new Animal(’Tom’);console.log(cat.prop); // testconsole.log(cat.name); // undefinedconsole.log(cat.__proto__ === Object.prototype); // trueconsole.log(cat.__proto__ === Animal.prototype); // false

如上,如果構(gòu)造函數(shù)return了一個對象,那么new操作后返回的是構(gòu)造函數(shù)return的對象。讓我們來實現(xiàn)下這個特性,最終版代碼如下:

// construct: 構(gòu)造函數(shù)function newFunction() { var res = {}; // 排除第一個構(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); // testconsole.log(cat.name); // undefinedconsole.log(cat.__proto__ === Object.prototype); // trueconsole.log(cat.__proto__ === Animal.prototype); // false

以上代碼就是我們最終對new操作符的模擬實現(xiàn)。我們再來看下官方對new的解釋

引用MDN對new運算符的定義:

new 運算符創(chuàng)建一個用戶定義的對象類型的實例或具有構(gòu)造函數(shù)的內(nèi)置對象的實例。

new操作符會干下面這些事:

創(chuàng)建一個空的簡單JavaScript對象(即{}); 鏈接該對象(即設(shè)置該對象的構(gòu)造函數(shù))到另一個對象 ; 將步驟1新創(chuàng)建的對象作為this的上下文 ; 如果該函數(shù)沒有返回對象,則返回this。

4條都已經(jīng)實現(xiàn)。還有一個更好的實現(xiàn),就是通過Object.create去創(chuàng)建一個空的對象:

// construct: 構(gòu)造函數(shù)function newFunction() { // 通過Object.create創(chuàng)建一個空對象; var res = Object.create(null); // 排除第一個構(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 国产色视频一区二区三区 | 她也啪在线视频精品网站 | 亚洲视屏在线观看 | 国产成人精品本亚洲 | 男人的天堂免费视频 | 欧美成人免费xxx大片 | 久久久久久久综合色一本 | 在线观看亚洲精品专区 | 亚洲成人黄色网 | 在线免费视频 | 亚洲精选在线观看 | 免费一区二区三区 | 国产日韩精品一区在线观看播放 | 久草社区视频 | a级毛片毛片免费观看永久 a级毛片毛片免费很很综合 | 亚洲国产精品视频 | 欧美成人精品第一区 | 免费视频99 | 久久免费精品一区二区 | 精品久久久日韩精品成人 | 欧美韩国xxx | 成人免费看www网址入口 | 亚洲欧美日本国产综合在线 | 狼人激情网 | 91www成人久久| 欧美xxxx性xxxxx高清视频 | 美女视频永久黄网站在线观看 | 一级黄色欧美片 | 最新中文字幕视频 | freesex寂寞老妇hd | 一级看片免费视频囗交 | 91原创视频在线观看 | 亚洲人的天堂男人爽爽爽 | 欧美一级在线 | 国产精品短视频免费观看 | 亚洲精品久久9热 | 成年女人毛片免费观看中文w | 在线观看视频亚洲 | 亚洲欧美日韩天堂 | 亚洲精品视 | 99免费精品 |