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?

Praveen
  • 55,303
  • 33
  • 133
  • 164
Kevin
  • 73
  • 5
  • may you can take a look at this question http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – rab Nov 05 '13 at 07:29
  • 2
    Inside an IIFE (Immediately Invoked Function Expression) the variable scope is *private* and for solely use inside your function scope. – Roko C. Buljan Nov 05 '13 at 07:29
  • Try with `window.baz = 1;` http://jsbin.com/OcifoKo/1/edit – Roko C. Buljan Nov 05 '13 at 07: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. – user1600124 Nov 05 '13 at 07:35
  • Any variable declared with `var` is only accessible inside the function it was defined in. – Felix Kling Nov 05 '13 at 08:05

3 Answers3

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).

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • thank you I missed immediate invoked function expressions.I get it now thank you very much – Kevin Nov 05 '13 at 07:40
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/

Alex
  • 11,115
  • 12
  • 51
  • 64
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 //
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313