1
var a = 1;

function hello() {
   (function(){
      alert(a);
   }());
}

Or is the scope chain only used when objects have been chained together using the prototype property?

Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
  • 3
    What is your specific question about the code you posted? What do you expect will happen and what actually happens? – Adam Jenkins Sep 08 '15 at 15:24
  • What I expect happens is what happens. – Ian Warburton Sep 08 '15 at 15:27
  • 1
    Yeah it does uses scope chain since variable `a` is not present locally so it searches it towards global scope. `prototype` property has nothing to do with it. @Adam, @Ian Warburton read [this](http://stackoverflow.com/questions/1484143/scope-chain-in-javascript) for understanding scope chain. – guleria Sep 08 '15 at 15:28
  • When using prototype inheritance, surely it also navigates up that hierarchy via the prototype property in the same way it looks up the function scopes? – Ian Warburton Sep 08 '15 at 15:33

2 Answers2

4

Yes the scope chain is used. The scope chain is more related to closures.

When hello is called, for getting the value of a for alert(a), the inner anonymous function is searched for the variable a.

Not finding anything, it moves up to the function hello and then one level up to the function / global scope in which hello is defined.

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • How does it navigate up one level? – Ian Warburton Sep 08 '15 at 15:32
  • 1
    Whenever a function is created it records the scope it was created in (in your case both the functions (hello and the inner one, when hello is called) would do that. Also, when a function is called, an environment is created for the new scope (since JavaScript is function scoped) that is entered - this has a pointer to the outer scope's environment. To resolve a variable the complete environment chain is traversed. For a detailed explanation (with diagrams) check out http://speakingjs.com/es5/ch16.html#variable_scope > Lexical dimension: chain of environments – potatopeelings Sep 08 '15 at 15:39
  • There are two chains aren't there? A scope chain and a prototype chain? If I run 'myObj.a' then it will look up the prototype chain. Where as if I run just 'a' then it will look up the scope chain. Is that correct? – Ian Warburton Sep 08 '15 at 15:55
  • Aha... this is what I'm looking for... http://stackoverflow.com/questions/27434357/scope-chain-look-up-vs-prototype-look-up-which-is-when – Ian Warburton Sep 08 '15 at 16:21
  • ah ok! variables vs properties! glad you got it figured out. – potatopeelings Sep 08 '15 at 23:34
4

I'm not sure if I understand right your question but I will try to answer. You declare a variable in global scope a. After you have a function declaration and an IIFE as closure in hello. When you call hello the IIFE will execute and because in local scope there isn't any variable a will go up one level to global scope. So alert 1.

var a = 1;

function hello() {
  (function() {
    alert(a);
  }());
}

hello();//alerts 1

In the next example I declare a local variable a so alert will be 3.

    var a = 1;

    function hello() {
      var a = 3;
      (function() {
        alert(a);
      }());
    }

    hello(); //alerts 3
Alex Char
  • 32,879
  • 9
  • 49
  • 70
  • Do you know how this is structured in the background? Does it create a chain of nested objects and follow the pointers until it finds what its looking for? – Ian Warburton Sep 08 '15 at 15:31
  • 1
    @Ian It probably varies between the different javascript implementations. I know I've seen an answer on here actually written by one of the V8 developers - that's the sort of input you'd need here – James Thorpe Sep 08 '15 at 15:33
  • @IanWarburton no. Think closures in javascript like a building with floors. First floor is the current scope. When you try to find an info and you can't you go up one floor until you reach the final floor(window object). – Alex Char Sep 08 '15 at 15:34