最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Is there a Javascript variable that represents local scope? Like global? - Stack Overflow

programmeradmin3浏览0评论

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?

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?
Share Improve this question edited Nov 18, 2011 at 14:31 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Nov 18, 2011 at 14:10 Sean Clark HessSean Clark Hess 16.1k13 gold badges54 silver badges102 bronze badges 10
  • 2 Is this for debugging? I can't imagine any real-world scenario where this would be useful... – lonesomeday 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 es from? For example, what could prime number EVER be useful for? How could anyone ever need more than 64KB of RAM? Etc.. – Jakob 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; } – Jakob 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! – Jakob 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. – Jakob Commented Nov 18, 2011 at 19:15
 |  Show 5 more ments

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论