jQuery new Object .bind
I've come across a code snippet that i am trying to understand. I did not find it anywhere. The code still works fine. Its about the .bind function of jQuery:
$(new Object()).bind()
what does the bind function of jQuery do with " new Object() ". Can someon开发者_如何学运维e elaborate?! Thanks.
That statement by itself won't do anything, really, except possibly result in a runtime error. The jQuery "bind()" method requires at least one parameter.
Even with a parameter, it won't do anything, however, because it's all about event handler binding. It doesn't make sense to do that to a plain JavaScript object.
The "bind()" call is normally used like this (though there are variations):
$('selector').bind('event-name', function(ev) { /* event handler code */ });
The "selector" string is used to find one or more DOM elements in a page.
$(new Object()).bind("something", function(event, data){ });
This means that you are binding a function delegate with parameters "event" and "data" to new object's "something" event. Whenever the newly created object fires "something" event, the function will execute.
I've got it solved. For someone who is interested-> There are two parts to it:
<a id="refButton" href="#">Click Me !!</a>
**Part 1:**
$(new Object()).bind("pressMe", function(event, data){
$(document).ready(function(){
$('#refButton').bind("click",function(){
alert("Hi There !!");
});
});
});
**Part 2**
if(some condition == TRUE){
$.event.trigger("pressMe");
}
So, everybody knows that "$(document).ready(function(){" gets executed first - but - in this way only when Part2 condition is TRUE than it triggers the "pressMe" event defined in Part1 which ultimately listens to the click event of the #refButton Id.
But it still remains a mistery to me how the empty new Object() - disables the "$(document).ready(function(){" functionality till the trigger is activated.
But thanks to all who answered.
精彩评论