Javascript foo()和函数()之间的区别{foo();}
用匿名函数包装函数有什么好处吗? 我指的是一个特别的例子:Javascript foo()和函数()之间的区别{foo();},javascript,Javascript,用匿名函数包装函数有什么好处吗? 我指的是一个特别的例子: function asyncFuntion(callback) { setTimeout(callback, 6000); }; asyncFuntion(function() { console.log('Calling after 6 s.'); }); function CustomObject() { this.msg = "Hello world from my custom object";
function asyncFuntion(callback) {
setTimeout(callback, 6000);
};
asyncFuntion(function() {
console.log('Calling after 6 s.');
});
function CustomObject() {
this.msg = "Hello world from my custom object";
this.alert = function() {
alert(this.msg);
};
}
var customObj = new CustomObject();
setTimeout(customObj.alert, 1000); // the alert message says `undefined`
setTimeout(function() {
customObj.alert();
}, 2000); // the alert message says "Hello world from my custom object"
使用包装函数:
function asyncFuntion(callback) {
setTimeout(function() {
callback();
}, 6000);
};
asyncFuntion(function() {
console.log('Calling after 6 s.');
});
在这两种情况下,输出是相同的。那么有什么区别吗?
第二个版本是我在学习js时发现的。
我意识到,当我们需要闭包时,这种形式是有用的,但是这里呢 第二种形式允许您将参数传递给
回调
,而第一种形式不允许
// 1st form
setTimeout(callback("This doesn't work as you might expect"), 6000);
// 2nd form
setTimeout(function() {
callback("This works");
}, 6000);
如果不传递参数,那么包装函数没有任何好处
更详细地说,可以用第一种形式帮助我们:
setTimeout(callback.bind(this, "This works fine too"), 6000);
// Even with Richard JP Le Guen's example by specifying the thisObj
setTimeout(customObj.alert.bind(customObj), 6000);
但是,您需要为不支持该事件的浏览器(即Opera、Safari和IE 8、7、6)提供此方法。填充方法的代码可在MDN文档页面上找到。将函数包装在匿名函数中可以避免使用
此
关键字的复杂性。()
例如:
function asyncFuntion(callback) {
setTimeout(callback, 6000);
};
asyncFuntion(function() {
console.log('Calling after 6 s.');
});
function CustomObject() {
this.msg = "Hello world from my custom object";
this.alert = function() {
alert(this.msg);
};
}
var customObj = new CustomObject();
setTimeout(customObj.alert, 1000); // the alert message says `undefined`
setTimeout(function() {
customObj.alert();
}, 2000); // the alert message says "Hello world from my custom object"
在匿名函数中包装函数也是在JavaScript中使用的关键:
var arr = ['a','b','c','d','e'];
// will always alert undefined
for(var i=0; i<arr.length; i++) {
setTimeout(function() {
console.log(arr[i]);
}, 1000*i);
}
// outputs the values of `arr`
for(var j=0; j<arr.length; j++) {
setTimeout((function(indx) {
return function() {
console.log(arr[indx]);
}
}(j)), 1000*j);
}
var arr=['a','b','c','d','e'];
//将始终警告未定义
for(var i=0;i如果需要单独的标识,则包装非常有用
var x = function () { cleanup(); };
var y = function () { cleanup(); };
if (x === y) ... // not true
element.addEventListener("myEvent", beep, false);
element.addEventListener("myEvent", beep, false);
例如,一些函数,如addEventListener
对标识进行操作
var x = function () { cleanup(); };
var y = function () { cleanup(); };
if (x === y) ... // not true
element.addEventListener("myEvent", beep, false);
element.addEventListener("myEvent", beep, false);
第二次调用addEventListener
时,它会说“我已经听到了嘟嘟声;我不需要再添加一个。”当myEvent
事件被触发时,你只会听到一声嘟嘟声。如果你想要两声嘟嘟声,你需要确保回调不同
element.addEventListener("myEvent", function() { beep(); }, false);
element.addEventListener("myEvent", function() { beep(); }, false);
每个匿名函数都是不同的,所以这次您注册了两个函数(正好做相同的事情)。现在它将发出两次蜂鸣声。setTimeout(callback.bind(这个“同样有效”),6000);
@jAndy:yes,但在没有垫片的旧浏览器中不能。谢谢您没有建议setTimeout(callback,6000,“这同样有效”)
,不过;)@AndyE你说它在旧浏览器中不起作用是什么意思?它们在函数中没有apply
。prototype
?@AndyE:lmao,甚至不知道setTimeout curry是回调函数的参数。我想如果你像这样评论它,这是不标准的?@Esailija:apply
不足以达到这个目的。W我们不希望立即执行回调,但我们希望在以后的某个时候绑定调用的上下文和参数。因此,bind()
是一个非常新的概念。+1,这是大多数人没有想到的边缘情况之一。FWIW,这是另一个Function.prototype.bind()
可能有用的场景。