以编程方式获取javascript函数的局部变量值?
问题描述:
鉴于:
function foo(){
var bar = "quux";
console.log(/*mystery code here*/);
}
我正在寻找插入评论时会产生价值的代码吧。举例来说,这样的东西在全球范围内起作用:
I'm looking for code that when inserted into the comment would yield the value of bar. By way of illustration, something like this works in a global scope:
var foo = "bar";
var bar = "quux";
console.log(window[foo]);
但是,当然,全局定义的变量会附加到窗口对象。函数的局部变量不是。是否有一些类似的方式以编程方式获取本地函数变量?
But, of course, variables defined globally are appended to the window object. Variables local to a function are not. Is there some similar way to programmatically get to local function variables?
答
不,不要害怕。