1

I read a lot of posts and ask/answer about javascript anonymous self-executing functions, but I'm afraid I'm still missing the point. Why does this code show myvar value? Shouldn't the construct (function(){ code })() keep all variables not visible from outside?

(function(){
    myvar = 5;
})();

alert(myvar);

so what's the difference betwen above code and

function myfunction(){
    myvar = 5;
};
myfunction();

alert(myvar);

?

1
  • 5
    Local variables should be defined with var keyword. Commented Jan 5, 2013 at 17:10

3 Answers 3

3

myvar is defined in window scope, hence it is accessible to all.

You should scope myvar IF you want to keep it private, with var:

(function(){
    var myvar = 5;
})();
Sign up to request clarification or add additional context in comments.

2 Comments

so how should I use the self.executing construct?
@user1846340 What you call a "self-executing function" is more accurately called an Immediately Invoked Function Expression, or IIFE. "Self-executing" suggests some type of recursion, in which the function calls itself repeatedly.
2

Variables are scoped at the function level in javascript. What this means is if you declare a variable inside a loop or an if statement, it will be available to the entire function.

In your case, your myVar is available to the whole window as somebody pointed out already.

If you need to explicitly cage your variable inside a function scope, create an anonymous function and then execute it straight away and all the variables inside will be scoped to the function

(function(){
    var myvar = 5;
})();


alert(typeof(myVar));

results "undefined"

5 Comments

so the construct (function(){})() has nothing to do with variable scoping...?
@user1846340 It does, but only as much as function foo() { }; foo(); does. Immediately-invoked functions follow the exact same scoping rules as any other function.
@user1846340 it does have. It creates a new scope. You just need to use the scope as well.
@user1846340 the point of a self-executing anonymous function is to keep the variables private inside its scope, to do some processing without the need for new members at global level. it just executes the code inside. in your POV, you can expose just some bits of your public API (example) and keep the core parts private like this.
esattemente quello che vorrei fare...cmq anche io odio facebook ;)
2

You failed to declare "myvar" with var.

See what happens when you change it like this:

(function(){
    "use strict";
    myvar = 5;
})();

3 Comments

I know I could declare it with var, but I'd like to understand the usage of self-executing functions. Using strict mode alert doesn't see myvar
@user1846340 The point is that if you don't declare it with var it is a global variable.
@user1846340 To spoil the suspense (:P), in strict mode, using a variable identifier without a var declaration is not allowed -- this function fails with the error ReferenceError: assignment to undeclared variable myvar. In strict mode, you can use global variables by explicitly referencing them as a property of the global object, like window.myvar.

Your Answer

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.