11

global is an object containing any global variables (at least in Node.js, they're in window in the browser).

Is there a similar variable that represents the current scope? Local variables don't show up in global (for a good reason :) )

asdf = "hello";
var local = "hello";

console.log(global); // includes asdf
console.log(???);    // includes local?
10
  • 2
    Is this for debugging? I can't imagine any real-world scenario where this would be useful... Commented Nov 18, 2011 at 14:11
  • 3
    I really don't want to sound negative, but I can't help but disliking the attitude "cant' imagine why you would want to do X". Where do you guys think innovation comes from? For example, what could prime number EVER be useful for? How could anyone ever need more than 64KB of RAM? Etc.. Commented Nov 18, 2011 at 14:43
  • I even made up an example. Lets say we have a function for swapping the values of two properties in an object, like this: var swap = function(target, a, b) { var temp = target[a]; target[a] = target[b]; target[b] = temp; } Commented Nov 18, 2011 at 14:44
  • 1
    But with the current limits of javascript, it's impossible to swap two local variables with each other. Even if things seems useless at first glance, there is always someone that can think of a useful scenario. "Useless" things are fantastic! Commented Nov 18, 2011 at 14:46
  • 1
    Exactly. Everything is about being responsible. Dynamic typing is dangerous too. Being able to change methods of any class at any time is dangerous as well. Some languages decides to protect the programmer from themselves (which is fine), but some languages decides to do the opposite: freedom/power/responsibility. It's not "bad". It's just different. Commented Nov 18, 2011 at 19:15

1 Answer 1

21

Is there an object represents the local scope?

Yes. There is.

Could you access the object (directly)?

No. You can't.

Why? JavaScript has only function scope - which is the execution Context. Within the execution Context, an Activation object(also known as call object) is used to create local variables as its property. However,

...it is not a normal object as it has no prototype (at least not a defined prototype) and it cannot be directly referenced by javascript code.

Reference

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

3 Comments

As many things in the specification, this object may be completely virtual, if the implementation chooses so. The main points is, it can not be used in the code.
@herby Yeah, it is. This is an abstract object in the specification.
Just as a side note, there are similar languages, for example Io (which is object-oriented, has prototypes, first-class functions etc) where you can access that object. Even if such unknown languages rarely are used for "real" projects, experimenting with them is useful as it teaches us how things could work (or actually work, behind the scenes).

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.