在特定作用域中调用函数
- apply(作用域,参数数组或arguments)
- call(作用域,参数1,参数2…)
创建一个函数的实例
- bind(作用域)
var color = 1;
var o = {
color:2
};
function s() {
console.log(this.color);
};
s();//1
s.bind(o)();//2
s.call(o);//2
s.apply(o);//2
var indexFun = {
arr_str : [1,'a','abc','66'],
mySort : function (a,b,c) {
console.log(this)
},
init:function(){
arr_str.sort(this.mySort.bind(indexFun));//this为indexFun
arr_str.sort(this.mySort.call(indexFun));//this为indexFun
arr_str.sort(this.mySort.apply(indexFun));//this为indexFun
arr_str.sort(this.mySort);//this为window
}
};
indexFun.init();