我正在尝试创建一个javascript表达式解析器,能够解析表达式并在其体内获取声明的变量,而不执行该函数:
function test(expressionFn) {
var expression = getExpression(expressionFn);
// get value expression.expressionRight in scope
// not work -> var valueRigthInScope = expressionFn.prototype.constructor[[[Scopes]]][expression.expressionRight];
console.log(expressionFn.prototype); // see ".constructor[[[Scopes]]]" in debug tools [F12]
}
function getExpression(expressionFn) {
var strAfterReturn = expressionFn.toString().split("return")[1].trim();
let strExpression = strAfterReturn.split(";")[0].split(" ");
return {
expressionLeft: strExpression[0],
operator: strExpression[1],
expressionRight: strExpression[2]
};
}
function start(){
var myValue = "This is value!";
test(function(x) { return x.prop1 == myValue; });
}
start();
<h1>See console</h1>
但是我无法访问函数的范围来获取变量myValue
的值,例如。
在Google Chrome控制台中,我通过该功能管理了获取声明变量myValue
的范围,但我无法在javascript中访问相同的范围。
按照Google Chrome控制台的图片显示:
如何在图片中访问[[Scopes]]
?