views:

167

answers:

2

I'm starting learning javascript for a project, I've found a script that does a part of what I need to do, I'd like to know how it works, both for me and in case it needs to be modified.

Originally it was used inside the page, now I've put it in a file on its own and does not work anymore, so I'm dividing it in parts, because I fail to get the whole thing.
Here is what bother me most for now:

1) Is this a declaration of a function? What is its name? How can it be invoked?

(function() {
    //some code
})();

2) No clue of what is going on here

var VARIABLE = VARIABLE || {};

3) Am I defining the implementation of methodCall here? Something like overriding a method in Java?

VARIABLE.methodCall = function(parameter) {
    console.log("parameter was: " + parameter);
};

Thank you in advance for your help.

+10  A: 

1) creates an unnamed function and executes it. this is useful for creating a scope for local variables that are invisible outside the function. You don't need to invoke it aside from this, the '()' at the end does that for you.

2) if variable is null/undefined, set it to an empty object.

3) yes, that should work as you expect, you can call VARIABLE.methodCall(parameter)

in response to your comment, here is a common example

function foo (VARIABLE)  {
   var VARIABLE = VARIABLE || {};
}
Jimmy
Thanks for your answer, how can `VARIABLE` be null/undefined if it is definined in that line?
Alberto Zaccagni
Ok I see, now it's clear. Thanks.
Alberto Zaccagni
Just a note, in your second example, `VARIABLE` will be *always* initialized to `{}`, regardless the value of the outer `VARIABLE`, because the outer will be *shadowed* in the scope created by the function.
CMS
@CMS: :) correct. deleting that example
Jimmy
@CMS: what does *shadowed* means in this case?
Alberto Zaccagni
@Montecristo, means that the outer variable is not accessible, because in the scope of the function, the identifier `VARIABLE` already exists, so you can't access the value of the outside scope.
CMS
@CMS: Ok, thanks.
Alberto Zaccagni
+3  A: 
(function() {
    //some code
})();

simply runs //some code, but variables in it will not remain, since the function() { } block introduces a new inner scope.

function() { } notation is called a closure, it allows variables be functions. For example,

(function() { })() is a common JavaScript idiom. After the ), there is (), which invokes the expression before as function, so (callback || function(x) { return x; })(x) is allowed.

var a = function a() { return 1; }

var VARIABLE = VARIABLE || {}; uses the short-circuit OR, If VARIABLE is not defined, VARIABLE will be set to {}, an empty object. (otherwise, if VARIABLE exists, it will not change)

x = A || B means "If A evaluates to TRUE, x is A, otherwise, x is B.".

VARIABLE.methodCall, as you said, adds methodCall to VARIABLE, without erasing other values in VARIABLE

SHiNKiROU
I don't think "closure" is the right term to name any function expression, a closure is formed only when a function declared inside another is made accessible *outside* of this function, binding its *enclosing scope*...
CMS