I'm trying to create a javascript expression parser, able to parse an expression and get the declared variables in its body, without executing the function:
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>
But I can not access the scope of the function to get the value of the variable myValue
, for example.
In the Google Chrome console I managed through the function to get the scope where the variable myValue
is declared, but I can not access that same scope in javascript.
Follows the image of the Google Chrome console:
How can I access [[Scopes]]
in the image?