0

Self invoking Coffeescript

$ -> 
  alert "Hello CoffeeScript!"

Compiles to

$(function() {
  return alert("Hello CoffeeScript!");
});

Now, the same code -- $ replaced with any other variable -->

hello= -> 
  alert "Hello CoffeeScript!"

Above code - Does not execute by itself.

How does giving a $ variable enable self execution(jQuery lib not included)? (is jQuery is playing a role here?)

I know you've to use do to have self invoking functions in coffeescript --- that's not my question ( please do not duplicate it ).

-coffeescript beginner

Vivek Chandra
  • 4,240
  • 9
  • 33
  • 38
  • I assume the `=` after `hello` is a typo? Otherwise, you *haven't* just swapped `hello` in for `$`, you've changed the code. – T.J. Crowder Jan 11 '15 at 13:32

2 Answers2

3

The code is not self-executing. It's just giving jQuery a function reference. It's jQuery that calls that function, later, when the DOM is ready (or right away if it already is).

Details in the jQuery documentation.

How does giving a $ variable enable self execution(jQuery lib not included)?

If the function is being called, jQuery clearly is included (or something else that defines a function and attaches it to $ and calls the function you pass it).

(is jQuery is playing a role here?)

Yes.


I know you've to use do to have self invoking functions in coffeescript

If you mean immediately-invoked rather than self-invoked (a common misnomer), no, you don't:

(() ->
  alert 'Hello!'
)()

...which translates to:

(function() {
  return alert('Hello!');
})();

...which defines a function and invokes it immediately.

Or if you really meant self-invoking (e.g., recursive):

(foo = (n) ->
  alert 'Call ' + n
  if n > 0
    foo(n - 1);
)(10)

...which translates to:

var foo;

(foo = function(n) {
  alert('Call ' + n);
  if (n > 1) {
    return foo(n - 1);
  }
})(10);

...which defines a function that calls itself 10 times.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I was trying the code on coffeescript.org's complier, $( callback ) is calling the function - then, coffeescript.org's comipler has jquery included by default? – Vivek Chandra Jan 11 '15 at 13:54
  • 2
    @VivekChandra: No, it's not the compiler. The compiler does not know anything about `$` (nor about jQuery). It just compiles your coffeescript code to an invocation of the `$` function. *When that is executed*, jQuery does its job. – Bergi Jan 11 '15 at 13:56
  • @bergi So, jQuery library is present in the coffeescript's tryCoffeescript tab. Understood, thanks. – Vivek Chandra Jan 11 '15 at 14:06
  • 1
    @VivekChandra: That's right, it's the CoffeeScript website that has it, a fairly outdated version. Try `alert typeof jQuery` in it (shows "object"), or `alert jQuery.fn.jquery` which will show you (at present) `"1.6.4"`. :-) – T.J. Crowder Jan 11 '15 at 14:27
  • @T.J.Crowder Wow, that confirms it. Thanks :D – Vivek Chandra Jan 11 '15 at 14:38
0

No, there is nothing special about the $ identifier. The actual difference between your snippets is the = after the identifier - without, it calls it with a function as the argument, with it the function gets assigned.

hello -> 
  alert "Hello CoffeeScript!"
// compiles to:
hello(function() {
  return alert("Hello CoffeeScript!");
});

hello = -> 
  alert "Hello CoffeeScript!"
// compiles to:
var hello;
hello = function() {
  return alert("Hello CoffeeScript!");
};

So if your hello function does invoke the given callback (as $ does), your function expression will get executed in the first case (without the assignment).

Notice that this is not a "self-invocation", as it's hello/$ that would call the function. For an IIFE (which is usually referred to as "self-invoking"), you'd use Coffeescripts do keyword.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375