相当于Python的locals()的Javascript吗? [英] Javascript equivalent of Python's locals()?

查看:57
本文介绍了相当于Python的locals()的Javascript吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,可以使用内置函数locals()globals()获取当前范围内所有局部变量和全局变量的字典.是否有一些等效的方式在Javascript中执行此操作?例如,我想执行以下操作:

In Python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in Javascript? For instance, I would like to do something like the following:

var foo = function(){ alert('foo'); };
var bar = function(){ alert('bar'); };

var s = 'foo';
locals()[s](); // alerts 'foo'

这完全有可能吗?还是应该只使用本地对象进行查找?

Is this at all possible, or should I just be using a local object for the lookup?

推荐答案

  • locals()-否

    • locals() - No.

      globals()-是.

      globals() - Yes.

      window是对全局范围的引用,例如python中的globals().

      window is a reference to the global scope, like globals() in python.

      globals()["foo"]
      

      与以下相同:

      window["foo"]
      

      这篇关于相当于Python的locals()的Javascript吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆