admin管理员组文章数量:1328033
I know .on()
exists with jQuery and .bind()
should not be used in the future, considering that I have a version of jQuery greater than or equal to 1.7.
What I want to know is this: are there are any differences between attaching an anonymous function or named function to an event handler using .bind()
?
Example:
// Anonymous function
$(".warning").bind("click", function(){
alert("Hello");
});
// Named function
$(".warning").bind("click", foo);
function foo(){
alert("Hello");
}
Imagine that I have 100 div
's with the class warning
in my page. The function .bind()
will attach a new function to every handler with an anonymous function but will it be exactly the same with a named function in the very internal of JavaScript and jQuery?
Thank you.
I know .on()
exists with jQuery and .bind()
should not be used in the future, considering that I have a version of jQuery greater than or equal to 1.7.
What I want to know is this: are there are any differences between attaching an anonymous function or named function to an event handler using .bind()
?
Example:
// Anonymous function
$(".warning").bind("click", function(){
alert("Hello");
});
// Named function
$(".warning").bind("click", foo);
function foo(){
alert("Hello");
}
Imagine that I have 100 div
's with the class warning
in my page. The function .bind()
will attach a new function to every handler with an anonymous function but will it be exactly the same with a named function in the very internal of JavaScript and jQuery?
Thank you.
Share Improve this question edited Aug 3, 2016 at 18:02 The One and Only ChemistryBlob 7,88411 gold badges40 silver badges75 bronze badges asked Aug 21, 2012 at 14:07 SamuelSamuel 12.4k6 gold badges52 silver badges72 bronze badges1 Answer
Reset to default 10There won't be any noticeable performance difference.
One main difference is that with a named function you can also selectively unbind functions and not just all functions associated with an event type.
Of course, this can also help you avoid code duplication.
本文标签: javascriptEvent handler bind to an anonymous function vs named functionStack Overflow
版权声明:本文标题:javascript - Event handler bind to an anonymous function vs named function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742244981a2439071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论