Function.prototype.bind用法示例
示例 用法 Function bind prototype
2023-06-13 09:15:05 时间
//ECMAScript5Function.prototype.bind函数兼容处理
(function(){
if(!Function.prototype.bind){//function(){}.bind
Function.prototype.bind=function(o,/*参数列表*/){
varself=this,boundArgs=Array.prototype.slice.call(arguments,0);
returnfunction(){
varargs=[],i;
for(i=1;i<boundArgs.length;i++)args.push(boundArgs[i]);
for(i=0;i<arguments.length;i++)args.push(arguments[i]);
returnthis.apply(o,args);
}
}
}
})();
用法示例:
1、简单调用示例
/*example1*/
functionf1(y,z){returnthis.x+y+z;}
//调用1
varg1=f1.bind({x:1},2);//this.x=1;y=2;
console.loog(g1(3));//this.x+y+3=6;
//调用2
varg2=f1.bind({x:1});//this.x=1;
console.log(g2(2,3));//this.x+2+3=6
/*example2*/
varf2(x,y){returnx+y;}
//调用
varg3=f2.bind(null,1);//x=1
console.log(g3(2));//x+2=3
2、DOM调用示例
vareleBtn=document.getElementById("button")
,eleText=document.getElementById("text");
eleBtn.onclick=function(color){
color=color||"#003399";
this.style.color=color;//此时的this指向eleText
}.bind(eleText,"#cd0000");
相关文章
- python chmod_Python os.chmod用法及代码示例
- 【运筹学】表上作业法 ( 闭回路示例 )
- 【OpenGL】二十一、OpenGL 矩阵压栈与出栈 ( 不同类型矩阵变换先后顺序 | 渲染前不设置单位阵 | 压栈出栈原理分析 | 代码示例 )
- 【EventBus】EventBus 使用示例 ( 最简单的 EventBus 示例 )
- 2023-04-21:用go语言重写ffmpeg的metadata.c示例。
- oracle截取字符(substr)检索字符位置(instr)示例介绍
- Linux ln命令:示例与用法(linuxln命令实例)
- MySQL中logcat的用法作用以及示例详解(mysql中logcat)
- MySQL中exists函数的用法和示例说明(mysql中exsits)
- 利用Redis实现订阅发布模型的示例解析(redis订阅例子)
- “增强js程序代码的健壮性”之我见大量示例代码
- 用示例说明filter()与find()的用法以及children()与find()的区别分析
- Javascript合并表格中具有相同内容单元格示例
- setInterval()和setTimeout()的用法和区别示例介绍
- javascript特殊用法示例介绍
- C#通过接口与线程通信(捕获线程状态)示例代码
- ASP.NETDictionary的基本用法示例介绍
- JS中数组Array的用法示例介绍
- js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)
- updatepanel用法之triggers使用示例
- php中session使用示例
- OracleMERGEINTO的用法示例介绍
- jquery访问ashx文件示例代码