一起Polyfill系列:Function.prototype.bind的四个阶段
昨天邊參考es5-shim邊自己實現Function.prototype.bind,發現有不少以前忽視了的地方,這里就作為一個小總結吧。
一、Function.prototype.bind的作用
其實它就是用來靜態綁定函數執行上下文的this屬性,并且不隨函數的調用方式而變化。
示例:
二、瀏覽器支持
Function.prototype.bind是ES5的API,所以坑爹的IE6/7/8均不支持,所以才有了自己實現的需求。
三、實現:
第一階段
只要在百度搜Function.prototype.bind的實現,一般都能搜到這段代碼。
Function.prototype.bind = Function.prototype.bind|| function(){var fn = this, presetArgs = [].slice.call(arguments); var context = presetArgs.shift();return function(){return fn.apply(context, presetArgs.concat([].slice.call(arguments)));};};它能恰好的實現Function.prototype.bind的功能定義,但通過看es5-shim源碼就會發現這種方式忽略了一些細節。
第二階段
而第一階段的實現方式,調用bind所返回的函數的length屬性只能為0,而實際上應該為fn.length-presetArgs.length才對啊。所以es5-shim里面就通過bound.length=Math.max(fn.length-presetArgs.length, 0)的方式重設length屬性。
因此es5-shim中的實現方式是無效的。既然不能修改length的屬性值,那么在初始化時賦值總可以吧,也就是定義函數的形參個數!于是我們可通過eval和new Function的方式動態定義函數來。
簡單來說在函數體中調用eval,其代碼的執行上下文會指向當前函數的執行上下文;而new Function或Function中代碼的執行上下文將一直指向全局的執行上下文。
舉個栗子:
因此這里我們要是用eval來動態定義函數了。
具體實現:
現在成功設置了函數的length屬性了。不過還有些遺漏。
第三階段
test('ctor produced by native Function.prototype.bind', function(){
?var Ctor = function(x, y){
?? this.x = x;
?? this.y = y;
? };
? var scope = {x: 'scopeX', y: 'scopeY'};
? var Bound = Ctor.bind(scope);
? var ins = new Bound('insX', 'insY');
? ok(ins.x === 'insX' && ins.y === 'insY' && scope.x === 'scopeX' && scope.y === 'scopeY', 'no presetArgs');
? Bound = Ctor.bind(scope, 'presetX');
? ins = new Bound('insY', 'insOther');
? ok(ins.x === 'presetX' && ins.y === 'insY' && scope.x === 'scopeX' && scope.y === 'scopeY', 'with presetArgs');
});
行為如下:
下面是具體實現
Function.prototype.bind = Function.prototype.bind|| function(){var fn = this, presetArgs = [].slice.call(arguments); var context = presetArgs.shift();var strOfThis = fn.toString(); // 函數反序列化,用于獲取this的形參var fpsOfThis = /^function[^()]*\((.*?)\)/i.exec(strOfThis)[1].trim().split(',');// 獲取this的形參var lengthOfBound = Math.max(fn.length - presetArgs.length, 0);var boundArgs = lengthOfBound && fpsOfThis.slice(presetArgs.length) || [];// 生成bound的形參eval('function bound(' + boundArgs.join(',')+ '){'+ 'if (this instanceof bound){'+ 'var self = new fn();'+ 'fn.apply(self, presetArgs.concat([].slice.call(arguments)));'+ 'return self;' + '}'+ 'return fn.apply(context, presetArgs.concat([].slice.call(arguments)));'+ '}');return bound; };現在連構造函數作為使用方式都考慮到了,應該算是功德圓滿了吧!NO,上面的實現只是基礎的實現而已,并且隱藏一些bugs!
潛伏的bugs列表:
第四階段
針對第三階段的問題,最后得到下面的實現方式
if(!Function.prototype.bind){
?var _bound = function(){
?? if (this instanceof bound){
???var ctor = function(){};
???ctor.prototype = fn.prototype;
???var self = new ctor();
???fn.apply(self, presetArgs.concat([].slice.call(arguments)));
???return self;
??}
??return fn.apply(context, presetArgs.concat([].slice.call(arguments)));
?}
?, _boundStr = _bound.toString();
?Function.prototype.bind = function(){
?? var fn = this, presetArgs = [].slice.call(arguments);
?? var context = presetArgs.shift();
?? var strOfThis = fn.toString(); // 函數反序列化,用于獲取this的形參
?? var fpsOfThis = /^function[^()]((.?))/i.exec(strOfThis)[1].trim().split(',');// 獲取this的形參
?? var lengthOfBound = Math.max(fn.length - presetArgs.length, 0);
?? var boundArgs = lengthOfBound && fpsOfThis.slice(presetArgs.length) || [];// 生成bound的形參
??// 通過函數反序列和字符串替換動態定義函數
?? var bound = eval('(0,' + _boundStr.replace('function()', 'function(' + boundArgs.join(',') + ')') + ')');
?? return bound;
? };
四、性能測試
// 分別用impl1,impl2,impl3,impl4代表上述四中實現方式
var start, end, orig = function(){};
start = (new Date()).getTime();
Function.prototype.bind = impl1;
for(var i = 0, len = 100000; i++ < len;){
?orig.bind({})();
}
end = (new Date()).getTime();
console.log((end-start)/1000); // 輸出1.387秒
start = (new Date()).getTime();
Function.prototype.bind = impl2;
for(var i = 0, len = 100000; i++ < len;){
? orig.bind({})();
}
end = (new Date()).getTime();
console.log((end-start)/1000); // 輸出4.013秒
start = (new Date()).getTime();
Function.prototype.bind = impl3;
for(var i = 0, len = 100000; i++ < len;){
? orig.bind({})();
}
end = (new Date()).getTime();
console.log((end-start)/1000); // 輸出4.661秒
start = (new Date()).getTime();
Function.prototype.bind = impl4;
for(var i = 0, len = 100000; i++ < len;){
? orig.bind({})();
}
end = (new Date()).getTime();
console.log((end-start)/1000); // 輸出4.485秒
由此得知運行效率最快是第一階段的實現,而且證明通過eval動態定義函數確實耗費資源啊!!!
當然我們可以通過空間換時間的方式(Momoized技術)來緩存bind的返回值來提高性能,經測試當第四階段的實現方式加入緩存后性能測試結果為1.456,性能與第一階段的實現相當接近了。
五、本文涉及的知識點
六、總結
在這之前從來沒想過一個Function.prototype.bind的polyfill會涉及這么多知識點,感謝es5-shim給的啟發。
我知道還會有更優雅的實現方式,歡迎大家分享出來!一起面對javascript的痛苦與快樂!
原創文章,轉載請注明來自^_^肥仔John[http://fsjohnhuang.cnblogs.com]
本文地址:http://www.cnblogs.com/fsjohnhuang/p/3712965.html
(本篇完)
?如果您覺得本文的內容有趣就掃一下吧!捐贈互勉!
??
轉載于:https://www.cnblogs.com/fsjohnhuang/p/3712965.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的一起Polyfill系列:Function.prototype.bind的四个阶段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 详解 Spotlight on MySQ
- 下一篇: 中国剩余定理求解“六位教授必须首次都停止