十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本文轉(zhuǎn)載自微信公眾號「秋風(fēng)的筆記」,作者藍色的秋風(fēng) 。轉(zhuǎn)載本文請聯(lián)系秋風(fēng)的筆記公眾號。

創(chuàng)新互聯(lián)建站主營梅州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),梅州h5小程序制作搭建,梅州網(wǎng)站營銷推廣歡迎梅州等地區(qū)企業(yè)咨詢
最近在幫女朋友復(fù)習(xí) JS 相關(guān)的基礎(chǔ)知識,遇到不會的問題,她就會來問我。
這不是很簡單?三下五除二,分分鐘解決。
- function bind(fn, obj, ...arr) {
- return fn.apply(obj, arr)
- }
于是我就將這段代碼發(fā)了過去
這時候立馬被女朋友進行了一連串的靈魂拷問。
這個時候,我馬老師就坐不住了,我不服氣,我就去復(fù)習(xí)了一下 bind,發(fā)現(xiàn)太久不寫基礎(chǔ)代碼,還是會需要一點時間復(fù)習(xí),這一次我得寫一個有深度的 bind,深得馬老師的真?zhèn)?,給他分成了五層速記法。
這一層非常的簡單,得益于 JS 原型鏈的特性。由于 function xxx 的原型鏈 指向的是 Function.prototype , 因此我們在調(diào)用 xxx.bind 的時候,調(diào)用的是 Function.prototype 上的方法。
- Function.prototype._bind = function() {}
這樣,我們就可以在一個構(gòu)造函數(shù)上直接調(diào)用我們的bind方法啦~例如像這樣。
- funciton myfun(){}
- myfun._bind();
想要詳細理解這方面的可以看這張圖和這篇文章(https://github.com/mqyqingfeng/blog/issues/2)
這可以說是 bind 最核心的特性了,就是改變 this 的指向,并且返回一個函數(shù)。而改變 this , 我們可以通過已知的 apply 和 call 來實現(xiàn),這里我們就暫且使用 apply 來進行模擬。首先通過 self 來保存當(dāng)前 this,也就是傳入的函數(shù)。因為我們知道 this 具有 隱式綁定的規(guī)則(摘自 《你不知道的JavaScript(上)》2.2.2 ),
- function foo() {console.log(this.a)}
- var obj = {a: 2, foo};
- obj.foo(); // 2
通過以上特性,我們就可以來寫我們的 _bind 函數(shù)。
- Function.prototype._bind = function(thisObj) {
- const self = this;
- return function () {
- self.apply(thisObj);
- }
- }
- var obj = {a:1}
- function myname() {console.log(this.a)}
- myname._bind(obj)(); // 1
可能很多朋友都止步于此了,因為在一般的面試中,特別是一些校招面試中,可能你只需要知道前面兩個就差不多了。但是想要在面試中驚艷所有人,仍然是不夠的,接下來我們繼續(xù)我們的探索與研究。
函數(shù)柯里化是一個老生常談的話題,在這里再復(fù)習(xí)一下。
- function fn(x) {
- return function (y) {
- return x + y;
- }
- }
- var fn1 = fn(1);
- fn1(2) // 3
不難發(fā)現(xiàn),柯里化使用了閉包,當(dāng)我們執(zhí)行 fn1 的時候,函數(shù)內(nèi)使用了外層函數(shù)的 x, 從而形成了閉包。
而我們的 bind 函數(shù)也是類似,我們通過獲取當(dāng)前外部函數(shù)的 arguments ,并且去除了綁定的對象,保存成變量 args,最后 return 的方法,再一次獲取當(dāng)前函數(shù)的 arguments, 最終用 finalArgs 進行了一次合并。
- Function.prototype._bind = function(thisObj) {
- const self = this;
- const args = [...arguments].slice(1)
- return function () {
- const finalArgs = [...args, ...arguments]
- self.apply(thisObj, finalArgs);
- }
- }
通過以上代碼,讓我們 bind 方法,越來越健壯了。
- var obj = { i: 1}
- function myFun(a, b, c) {
- console.log(this.i + a + b + c);
- }
- var myFun1 = myFun._bind(obj, 1, 2);
- myFun1(3); // 7
一般到了這層,可以說非常棒了,但是再堅持一下下,就變成了完美的答卷。
要知道,我們的方法,通過 bind 綁定之后,依然是可以通過 new 來進行實例化的, new 的優(yōu)先級會高于 bind(摘自 《你不知道的JavaScript(上)》2.3 優(yōu)先級)。
這一點我們通過原生 bind 和我們第四層的 _bind 來進行驗證對比。
- // 原生
- var obj = { i: 1}
- function myFun(a, b, c) {
- // 此處用new方法,this指向的是當(dāng)前函數(shù) myFun
- console.log(this.i + a + b + c);
- }
- var myFun1 = myFun.bind(obj, 1, 2);
- new myFun1(3); // NAN
- // 第四層的 bind
- var obj = { i: 1}
- function myFun(a, b, c) {
- console.log(this.i + a + b + c);
- }
- var myFun1 = myFun._bind(obj, 1, 2);
- new myFun1(3); // 7
注意,這里使用的是 bind方法
因此我們需要在 bind 內(nèi)部,對 new 的進行處理。而 new.target 屬性,正好是用來檢測構(gòu)造方法是否是通過 new 運算符來被調(diào)用的。
接下來我們還需要自己實現(xiàn)一個 new ,
而根據(jù) MDN,new 關(guān)鍵字會進行如下的操作:
1.創(chuàng)建一個空的簡單JavaScript對象(即{});
2.鏈接該對象(設(shè)置該對象的constructor)到另一個對象 ;
3.將步驟1新創(chuàng)建的對象作為this的上下文 ;
4.如果該函數(shù)沒有返回對象,則返回this。
- Function.prototype._bind = function(thisObj) {
- const self = this;
- const args = [...arguments].slice(1);
- return function () {
- const finalArgs = [...args, ...arguments];
- // new.target 用來檢測是否是被 new 調(diào)用
- if(new.target !== undefined) {
- // this 指向的為構(gòu)造函數(shù)本身
- var result = self.apply(this, finalArgs);
- // 判斷改函數(shù)是否返回對象
- if(result instanceof Object) {
- return reuslt;
- }
- // 沒有返回對象就返回 this
- return this;
- } else {
- // 如果不是 new 就原來的邏輯
- return self.apply(thisArg, finalArgs);
- }
- }
- }
看到這里,你的造詣已經(jīng)如火純情了,但是最后還有一個小細節(jié)。
以上的方法在大部分的場景下都沒有什么問題了,但是,當(dāng)我們的構(gòu)造函數(shù)有 prototype 屬性的時候,就出問題啦。因此我們需要給 prototype 補上,還有就是調(diào)用對象必須為函數(shù)。
- Function.prototype._bind = function (thisObj) {
- // 判斷是否為函數(shù)調(diào)用
- if (typeof target !== 'function' || Object.prototype.toString.call(target) !== '[object Function]') {
- throw new TypeError(this + ' must be a function');
- }
- const self = this;
- const args = [...arguments].slice(1);
- var bound = function () {
- var finalArgs = [...args, ...arguments];
- // new.target 用來檢測是否是被 new 調(diào)用
- if (new.target !== undefined) {
- // 說明是用new來調(diào)用的
- var result = self.apply(this, finalArgs);
- if (result instanceof Object) {
- return result;
- }
- return this;
- } else {
- return self.apply(thisArg, finalArgs);
- }
- };
- if (self.prototype) {
- // 為什么使用了 Object.create? 因為我們要防止,bound.prototype 的修改而導(dǎo)致self.prototype 被修改。不要寫成 bound.prototype = self.prototype; 這樣可能會導(dǎo)致原函數(shù)的原型被修改。
- bound.prototype = Object.create(self.prototype);
- bound.prototype.constructor = self;
- }
- return bound;
- };
以上就是一個比較完整的 bind 實現(xiàn)了,如果你想了解更多細節(jié)的實踐,可以查看。(也是 MDN 推薦的)
https://github.com/Raynos/function-bind