ES5中this绑定
<script>
/**
* this绑定
* 函数自调用,函数体中的this是指向window的,可以使用apply、cal、bind来绑定this
*/
var obj = {name:'tom'};
function say(data){
console.log(this,data);
}
say('111');//Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …} "111"
say.apply(obj,['111']);//{name: "tom"} "111"
say.call(obj,'111');//{name: "tom"} "111"
say.bind(obj,'111')();//{name: "tom"} "111"
console.log(say.bind(obj,'111'));//f say(data){console.log(this,data);}
//一般bind常用在回掉函数中,
setTimeout(function(){
console.log(this);
},1000);//Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
setTimeout(function(){
console.log(this);
}.bind(obj),1000);//{name: "tom"}
</script>