lkahtz
lkahtz

Reputation: 4796

equivalent ready functions in jQuery

Are these three equivalent?

$(function(){
  $("#a").html("i am a")
});

(function($){
  $("#b").html("i am b")
})($);

(function(){
  $("#c").html("i am c")
})(); 

The code is available here.

Reference:

Upvotes: 1

Views: 67

Answers (2)

GolezTrol
GolezTrol

Reputation: 116110

The first one actually waits for the ready event, while the others do not.

The second one is used for aliasing JQuery, but is useless in the way it is used now. You pass $, which is received in the parameter $. This construct is usually used when another library is used that assigns a different value to $. In that case you can call function($){...}(jQuery), to still have the $ variable pointing to jQuery in the scope of the function.

Upvotes: 2

Kevin B
Kevin B

Reputation: 95022

No, the first one waits until the document is ready, the other two do not. They simply create a scope for the code to run in rather than running in the global scope.

Upvotes: 0

Related Questions