国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Function.prototype.bind用法示例

2024-05-06 15:52:00
字體:
來源:轉載
供稿:網友
想必大家對Function.prototype.bind并不陌生吧,下面為大家介紹下它的簡單調用及DOM調用,感興趣的朋友可以參考下

復制代碼 代碼如下:


//ECMAScript 5 Function.prototype.bind函數兼容處理
(function(){
if ( !Function.prototype.bind ) { //function(){}.bind
Function.prototype.bind = function ( o, /*參數列表*/ ) {
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、簡單調用示例

復制代碼 代碼如下:


/*example 1*/
function f1(y, z){ return this.x + y + z;}
//調用 1
var g1 = f1.bind({x:1}, 2); //this.x = 1; y = 2;
console.loog( g1(3) ); //this.x + y + 3 = 6;
//調用 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; }
//調用
var g3 = f2.bind(null, 1); //x = 1
console.log( g3(2) ); //x + 2 = 3


2、DOM調用示例

復制代碼 代碼如下:


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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 东平县| 林周县| 子长县| 精河县| 盐亭县| 西乌珠穆沁旗| 雅江县| 满城县| 长兴县| 巴青县| 文山县| 瓮安县| 来宾市| 湘阴县| 安岳县| 剑川县| 肇庆市| 凌源市| 凤翔县| 六安市| 阳高县| 浦县| 文化| 荆州市| 商洛市| 永春县| 兴文县| 新乐市| 定兴县| 高雄县| 泽州县| 运城市| 托克逊县| 饶河县| 嘉兴市| 阿坝| 大渡口区| 兴化市| 凤冈县| 澎湖县| 历史|