詳解JavaScript中new操作符的解析和實(shí)現(xiàn)
前言
new 運(yùn)算符是我們?cè)谟脴?gòu)造函數(shù)創(chuàng)建實(shí)例的時(shí)候使用的,本文來(lái)說(shuō)一下 new 運(yùn)算符的執(zhí)行過(guò)程和如何自己實(shí)現(xiàn)一個(gè)類(lèi)似 new 運(yùn)算符的函數(shù)。
new 運(yùn)算符的運(yùn)行過(guò)程
new 運(yùn)算符的主要目的就是為我們創(chuàng)建一個(gè)用戶(hù)定義的對(duì)象類(lèi)型的實(shí)例或具有構(gòu)造函數(shù)的內(nèi)置對(duì)象的實(shí)例(比如箭頭函數(shù)就沒(méi)有構(gòu)造函數(shù),所以是不能 new 的)。new 操作符的執(zhí)行大概有以下幾個(gè)步驟:
創(chuàng)建一個(gè)新的空對(duì)象 把新對(duì)象的 __proto__ 鏈接到構(gòu)造函數(shù)的 prototype 對(duì)象(每一個(gè)用戶(hù)定義函數(shù)都有一個(gè) prototype 屬性指向一個(gè)對(duì)象,該對(duì)象有一個(gè) constructor 屬性指向該函數(shù)),讓我們的公共屬性和方法可以從原型上繼承,不用每個(gè)實(shí)例都創(chuàng)建一次。 將第一步創(chuàng)建的新的對(duì)象作為構(gòu)造函數(shù)的 this 的上下文,執(zhí)行構(gòu)造函數(shù),構(gòu)造函數(shù)的執(zhí)行讓我們配置對(duì)象的私有屬性和方法。 執(zhí)行構(gòu)造函數(shù),如果構(gòu)造函數(shù)沒(méi)有返回值或者返回值不是一個(gè)對(duì)象,則返回 this。我么可以用代碼簡(jiǎn)單表示上面的邏輯:
function new_ (constr, ...rests) { var obj = {}; obj.__proto__ = constr.prototype; var ret = constr.apply(obj, rests); return isPrimitive(ret) ? obj : ret; //判斷構(gòu)造函數(shù)的返回值是否為對(duì)象,不是則直接返回創(chuàng)建的obj對(duì)象}
new 的實(shí)現(xiàn)
上面講了 new 運(yùn)算符的執(zhí)行過(guò)程,下面我們來(lái)自己動(dòng)手實(shí)現(xiàn)一個(gè) new 運(yùn)算符。
function new_(constr, ...rests) { if (typeof constr !== 'function') { throw 'the first param must be a function'; } new_.target = constr; var obj = Object.create(constr.prototype); var ret = constr.apply(obj, rests); var isObj = typeof ret !== null && typeof ret === 'object'; var isFun = typeof ret === 'function'; //var isObj = typeof ret === 'function' || typeof ret === 'object' && !!ret; if (isObj || isFun) { return ret; } return obj;}function Person(name, age) { this.name = name; this.age = age;}Person.prototype.say = function () { console.log(this.name);};var p1 = new_(Person, ’clloz’, ’28’)var p2 = new_(Person, ’csx’, ’31’)console.log(p1); //Person {name: 'clloz', age: '28'}p1.say(); //cllozconsole.log(p2); //Person {name: 'csx', age: '31'}p2.say(); //csxconsole.log(p1.__proto__ === Person.prototype); //trueconsole.log(p2.__proto__ === Person.prototype); //true
以上就是一個(gè)簡(jiǎn)單的 new 實(shí)現(xiàn),判斷是否為對(duì)象那里可能不是很?chē)?yán)謹(jǐn),不過(guò)沒(méi)有想到更好的方法。
一個(gè)小補(bǔ)充,在 mdn 的 Function.prototype.apply() 詞條中看到的直接把方法寫(xiě)到 Function.prototype 上,也是個(gè)不錯(cuò)的思路,F(xiàn)unction.prototype 在所以函數(shù)的原型鏈上,所以這個(gè)方法可以在每個(gè)函數(shù)上調(diào)用,方法內(nèi)部的 this 也是指向調(diào)用方法的函數(shù)的。
Function.prototype.construct = function (aArgs) { var oNew = Object.create(this.prototype); this.apply(oNew, aArgs); return oNew;};
強(qiáng)制用 new 調(diào)用構(gòu)造函數(shù)
function Clloz(...arguments) { if (!(this instanceof Clloz)) { return new Clloz(...arguments) }}
Tips
補(bǔ)充兩個(gè)關(guān)于 new 運(yùn)算符的知識(shí)點(diǎn)。
上面提到 new 的執(zhí)行過(guò)程的最后一步,如果構(gòu)造函數(shù)沒(méi)有返回值或者返回值不是一個(gè)對(duì)象,則返回 this。但是如果返回的是一個(gè) null 的話(huà),依然返回 this,雖然 null 也算是 object。 new 操作符后面的構(gòu)造函數(shù)可以帶括號(hào)也可以不帶括號(hào),除了帶括號(hào)可以傳遞參數(shù)以外,還有一個(gè)重要的點(diǎn)是兩種用法的運(yùn)算符優(yōu)先級(jí)不一樣,在JS運(yùn)算符優(yōu)先級(jí)這篇文章中有提到,帶參數(shù)的 new 操作符的優(yōu)先級(jí)是比不帶參數(shù)的要高的,new Foo() > Foo() > new Foo。一般不太會(huì)遇到,可能有些題目會(huì)問(wèn)這些問(wèn)題。
以上就是詳解JavaScript中new操作符的解析和實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于JavaScript new解析和實(shí)現(xiàn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Struts2獲取參數(shù)的三種方法總結(jié)2. JSP中Servlet的Request與Response的用法與區(qū)別3. IntelliJ IDEA刪除類(lèi)的方法步驟4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. vue cli4下環(huán)境變量和模式示例詳解7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. Django視圖類(lèi)型總結(jié)9. IntelliJ IDEA導(dǎo)入jar包的方法10. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
