Function.prototype.bind重写

因为ie8之下的浏览器不支持Function.prototype.bind,所以要进行重写

html代码:

<input type="button" value="Click me" id="btn">
<span id="text">我会变色</span>

js代码:

if(!Function.prototype.bind){
Function.prototype.bind = function(context){
var args = Array.prototype.slice.call(arguments),
self = this;


return function(){
console.log(context);context的值<span id="text" color="blue">我会变色</span>
console.log(self);self就是function(color){this.style.color = color || "red";}
return self.apply(context,args.slice(1));
}
}



}

var btn = document.getElementById("btn"),
 
    text = document.getElementById("text");
btn.onclick = function(color){
this.style.color = color || "red";
}.bind(text,"blue");
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值