4

I have a problem with the online document of the website of jquery about javascript scope.There are codes;

(function() {

var baz = 1;

var bim = function() {
    console.log( baz );
};

bar = function() {
    console.log( baz );
};

})();

And says:

console.log( baz ); // baz is not defined outside of the function

the thing that I don't understand is that even though baz is defined, why console.log(baz) is undefined. Because I think the scope is the same. Did I miss something?

5
  • may you can take a look at this question stackoverflow.com/questions/111102/… Commented Nov 5, 2013 at 7:29
  • 2
    Inside an IIFE (Immediately Invoked Function Expression) the variable scope is private and for solely use inside your function scope. Commented Nov 5, 2013 at 7:29
  • Try with window.baz = 1; jsbin.com/OcifoKo/1/edit Commented Nov 5, 2013 at 7:32
  • i don't think the question is really about how to make the variable accessible outside of the functional scope.. using window.baz will just pollute the global scope.. The example should really mean that although you can't do console.log outside of the IIFE, you can call bar, which is able to reference baz, because bar is also defined in the same scope. Commented Nov 5, 2013 at 7:35
  • Any variable declared with var is only accessible inside the function it was defined in. Commented Nov 5, 2013 at 8:05

3 Answers 3

5

The trap is the IIFE - immediate invoked function expressions which creates their own scope.

JS is using function scope

So baz is not defined outside that IIFE.

change this to :

(function() {

 window.baz = 1;     <----

var bim = function() {
    console.log( baz );
};

bar = function() {
    console.log( baz );
};

})();

And it will work.

p.s.

This is how jQuery attach ( when they finish with bla bla..) the $ /jQuery to the window. ( only that the window.$ is at its last lines).

Sign up to request clarification or add additional context in comments.

1 Comment

thank you I missed immediate invoked function expressions.I get it now thank you very much
0

the thing that I don't understand is that even though baz is defined, why console.log(baz) is undefined

It's undefined cause it's range is restricted (private) to the IIFE (Immediately Invoked Function Expression) scope.

If you chain your variable to the Window object, it'll be accessible globally.

DEMO

(function() {

  window.baz = 1; // Global
  var pub    = 2; // private

  var bim = function() {
    console.log( baz );
  };

  var bar = function() {
    console.log( pub ); 
  };

  bim(); // 1 //////////
  bar(); // 2 //////////

})(); 


console.log( baz ); // 1 //////////
console.log( pub ); // Ref.Error //

Comments

0

May be you are under "strict mode" ? In Chrome console everything is right. Calling of "baz" write 1 .

jsfiddle to check jsfiddle.net/Yjq2v/

1 Comment

He's running console.log( baz ); outside the IIFE scope

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.