I have the following code:
$(document).ready(function() {
var myFunc = function () {
alert('test');
};
myfunc();
jQuery('#type-selector').change(myFunc);
});
The purpose of this code is that I need function myFunc
to be executed on both document.ready
event and onChange
event. This code works perfectly, however I wonder is there a way to avoid the call myFunc();
and execut开发者_如何学Goe the function on its definition instead? In other words I need to keep the reference on function object and execute function "in one line".
You could trigger the change
event:
jQuery('#type-selector').change(function() {
alert('test');
}).change();
or
jQuery('#type-selector').change(myFunc).change();
You can write
jQuery('#type-selector').change(
(function() {
alert('test');
return arguments.callee;
})()
);
This function returns itself when called (return arguments.callee
).
I call the function immediately, then pass its return value 9which is the same function) to change
as the event handler.
This is confusing code; I don't recommend using it.
精彩评论