1

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();
};
jb10210
1,1765 silver badges16 bronze badges
asked Apr 6, 2012 at 9:20
1
  • yes both are similar. $.proxy(func, this) must returning function Commented Apr 6, 2012 at 9:22

3 Answers 3

2

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

answered Apr 6, 2012 at 9:25
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Apr 6, 2012 at 9:26

1 Comment

i think then it is even bad practice using $() , if i am already loading my modules after dom ready.
0

Is the same, is just a shorthand.

answered Apr 6, 2012 at 9:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.