Alex Rivera | Logout

What is the scope of a function in Javascript/ECMAScript?

Asked 2008-10-24T21:44:44.773
15

Today I had a discussion with a colleague about nested functions in Javascript:

function a() {
   function b() {
      alert('boo')
   }
   var c = 'Bound to local call object.'
   d = 'Bound to global object.'
}

In this example, trials point out that b is not reachable outside the body of a, much like c is. However, d is - after executing a(). Looking for the exact definition of this behaviour in the ECMAScript v.3 standard , I didn't find the exact wording I was looking for; what Sec.13 p.71 does not say, is which object the function object created by the function declaration statement is to be bound to. Am I missing something?

Edit
Report

1 Answer

4

As I understand it, these are equivalent as far as scoping is concerned:

function a() { ... }

and

var a = function() { ... }
answered 2008-10-24T21:53:03.273

Your Answer