Reputation: 803
Which one of these is considered better code and why and what is better on memory?
Say your jquery code looks like example one without bind();
$(document).ready(function(){
$('#someDiv').mouseenter(function(){
$('#someDiv').fadeTo('fast',1);
});
$('#someDiv').mouseleave(function(){
$('#someDiv').fadeTo('fast', .5);
});
});
Or with bind() method.
$(document).ready(function(){
$('#someDiv').bind({
mouseenter: function(){
$('#someDiv').fadeTo("fast",1);
},
mouseleave: function(){
$('#someDiv').fadeTo("fast",0.5);
)
})
});
Upvotes: 0
Views: 22
Reputation: 781721
There's no difference. All the different ways of binding event handlers in jQuery eventually call the same internal function to actually perform the binding. Here's a simplified view of how it works:
bind: function(bindings) {
for (event in bindings) {
return this.on(event, bindings[event]);
},
mouseenter: function(handler) {
return this.on("mouseenter", handler);
},
mouseleave: function(handler) {
return this.on("mouseleave", handler);
}
BTW, .bind()
is outdated, although it's not yet deprecated. .on()
is preferred unless you need to be compatible with jQuery versions before 1.7.
Upvotes: 2