1

Possible Duplicate:
What does $(function() {} ); do?

what exactly does the following syntax mean?

$(function() {..}

as in

$(function () {

    $(".add_folder").click(function () {

Does it means only defining anonymos function? or also executing it?

TIA

Community
  • 1
  • 1
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

3 Answers3

3

It means the same thing as $(document).ready(function() {});

When the document is loaded, it calls the passed in function. I don't find the $(function() {}) form very self documenting so I use the $(document).ready(function() {}); form in my code, though they do the same thing.

See the jQuery doc for reference.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

The first executes when in global scope, the other is Only defining. So it fires when the element is clicked

Derk Arts
  • 3,432
  • 2
  • 18
  • 36
0

I have linked to/closed for the related questions as to "what it does", as to "how" this works:

expression(...) always treats the result of expression as a function-object and attempts to invoke it. (If the result is not a function-object there is an error.)

function (...) {...} -- in an expression context -- evaluates to the anonymous function-object. It never invokes said function.

$ is just a "normal identifier" or, an expression in this case. (It is likely that $ === jQuery in this example.)

Thus, the above is semantically equivalent of (ignoring property pollution):

func = $;
anon_func = function () {...};
func(anon_func);

Exactly what func does with anon_func is up to func. In this case, because func and $ and jQuery evaluate to the same function-object, the behavior is defined by jQuery(callback).

Happy coding.