亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Function.prototype.bind用法示例

 更新時(shí)間:2013年09月16日 10:16:45   作者:  
想必大家對(duì)Function.prototype.bind并不陌生吧,下面為大家介紹下它的簡(jiǎn)單調(diào)用及DOM調(diào)用,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:

//ECMAScript 5 Function.prototype.bind函數(shù)兼容處理
(function(){
if ( !Function.prototype.bind ) { //function(){}.bind
Function.prototype.bind = function ( o, /*參數(shù)列表*/ ) {
var self = this, boundArgs = Array.prototype.slice.call(arguments, 0);
return function(){
var args = [], i;
for ( i = 1; i < boundArgs.length; i++ ) args.push(boundArgs[i]);
for ( i = 0; i < arguments.length; i++ ) args.push(arguments[i]);
return this.apply(o, args);
}
}
}
})();

用法示例:
1、簡(jiǎn)單調(diào)用示例
復(fù)制代碼 代碼如下:

/*example 1*/
function f1(y, z){ return this.x + y + z;}
//調(diào)用 1
var g1 = f1.bind({x:1}, 2); //this.x = 1; y = 2;
console.loog( g1(3) ); //this.x + y + 3 = 6;
//調(diào)用 2
var g2 = f1.bind({x:1}); //this.x = 1;
console.log( g2(2,3) ); //this.x + 2 + 3 = 6

/*example 2*/
var f2(x, y){ return x + y; }
//調(diào)用
var g3 = f2.bind(null, 1); //x = 1
console.log( g3(2) ); //x + 2 = 3

2、DOM調(diào)用示例
復(fù)制代碼 代碼如下:

var eleBtn = document.getElementById("button")
, eleText = document.getElementById("text");

eleBtn.onclick = function(color) {
color = color || "#003399";
this.style.color = color; //此時(shí)的this指向eleText
}.bind(eleText, "#cd0000");

相關(guān)文章

最新評(píng)論