nitesh sharma
nitesh sharma

Reputation: 601

what does jQuery(function) do

I am reading javascript web application and the author is using following code:

mod.load = function(func){
    $($.proxy(func, this));
};

Can someone help me to understand why returning function from jQuery.proxy is inside jQuery wrapper.

Is this the same as:

mod.load = function(func){
    var temp = $.proxy(func, this);
    temp();
};

Upvotes: 1

Views: 285

Answers (3)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

Calling $() with a function argument is equivalent to applying $(document).ready() to that function: it waits for the DOM to be ready before calling it.

Therefore, in your second example, temp() may be called before the DOM is ready, depending on the moment when mod.load() itself runs.

Upvotes: 1

Armatus
Armatus

Reputation: 2191

They are not the same but they have the same effect. Your second example executes the returned function directly while jQuery(function) binds it to the onload like $(document).ready(). mod.load probably is the onload however, so this makes no difference.

See http://api.jquery.com/jQuery/#jQuery3

Upvotes: 2

ilyes kooli
ilyes kooli

Reputation: 12043

Is the same, is just a shorthand.

Upvotes: 0

Related Questions