【javascript之this】
1、如果作为Method使用,this指向调用它的对象。
2、如果作为Function使用,非严格模式下this指向全局变量,严格模式下为undefined。
3、如果作为Function调用,想访问外部函数的this值,需要将this保存在一个变量里,如下例:
4、after方法
创建一个新的函数,当func反复调用时,count次才调用一次,比如:
function a(){
alert("a");
}
var afterA = _.after(3,a);
afterA();//调用
afterA();//不alert
afterA();//不alert
afterA();//调用
alert("a");
}
var afterA = _.after(3,a);
afterA();//调用
afterA();//不alert
afterA();//不alert
afterA();//调用
源码:
_.after = function(times, func) {
if (times <= 0) return func();
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};
if (times <= 0) return func();
return function() {
if (--times < 1) {
return func.apply(this, arguments);
}
};
};