国产探花免费观看_亚洲丰满少妇自慰呻吟_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");

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

圖片精選

主站蜘蛛池模板: 蓬莱市| 揭东县| 广河县| 凌云县| 屯留县| 浦县| 巨鹿县| 饶河县| 衡南县| 习水县| 安达市| 惠东县| 赞皇县| 漠河县| 武邑县| 固阳县| 伽师县| 阿瓦提县| 蒙城县| 始兴县| 天门市| 元氏县| 安阳县| 桐城市| 游戏| 张北县| 沙洋县| 西宁市| 维西| 蓝山县| 甘南县| 荥阳市| 吴忠市| 九龙城区| 苗栗市| 时尚| 泰安市| 确山县| 大庆市| 诸城市| 广水市|