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

Access variable from code run in "eval" in Javascript - Stack Overflow

programmeradmin10浏览0评论

I know using eval is not at all remended and I have read this link too. Set Variable inside Eval (JavaScript)

However, this is what I want to do. Lets say we have some code in a textbox. So I have to take that text, and then find out all the global variables, functions and objects in that code. My idea was to wrap the code in a namespace, eval it and then iterate through the properties of the namespace to get the results. However, even though the eval runs successfully, I can't access the variable defined there. Is there a better solution or some other way to get this working.

/ - This is the Fiddle here.

The "var code" could actually be arbitrary code. I know its unsafe to do it but I need it for a different context. Thanks

I know using eval is not at all remended and I have read this link too. Set Variable inside Eval (JavaScript)

However, this is what I want to do. Lets say we have some code in a textbox. So I have to take that text, and then find out all the global variables, functions and objects in that code. My idea was to wrap the code in a namespace, eval it and then iterate through the properties of the namespace to get the results. However, even though the eval runs successfully, I can't access the variable defined there. Is there a better solution or some other way to get this working.

http://jsfiddle/DbrEF/2/ - This is the Fiddle here.

The "var code" could actually be arbitrary code. I know its unsafe to do it but I need it for a different context. Thanks

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Mar 23, 2012 at 2:48 ssarangissarangi 6121 gold badge10 silver badges30 bronze badges 1
  • close voter - how is this too localized? – Thomas Shields Commented Mar 23, 2012 at 3:08
Add a ment  | 

4 Answers 4

Reset to default 5

In 2015, creating a Function object is your best bet here, rather than using eval:

new Function('arg1', 'arg2', 'return arg1 + arg2')(3,4) // returns 7

You might have better luck using a Javascript parser, like the one used by JSHint/JSLint

here's a demo on safely using eval using "use strict"

window.onload = function(){
    'use strict'; 
    //use strict forces to run code in "strict mode" 
    //use strict prevents eval from 
    //contaminating the immediate scope

    //let's test with "foo"
    var foo = 'lol';

    //now code has "foo" but using "use strict" 
    //makes code in eval stay in eval
    //note that at the last of the code, foo is "returned"
    var code = 'var foo = {name: "Spock",greeting: function() {return "Hello " + foo.name;}}; foo';

    //store eval'ed code in evalO
    var evalstore = eval(code);

    console.log(evalstore); //code in eval stays in here, which is "inner foo"
    console.log(foo);       //"outer foo" is unharmed and daisy fresh
}​;

so whatever code you have, contain it in a function which will serve as your namespace. then have that function returned to the outside world stored as a variable. this demo shows how it can be constructed, however, works only if code is in object literal notation.

window.onload = function() {
    'use strict';

    var ns = 'lol';

    //code must be an object literal
    var code = '{name: "Spock",greeting: function(){return "Hello " + foo.name;}}';

    //store in a constructor to be returned
    var constructorString = 'var ns = function(){return ' + code + '}; ns'; 

    var evalNs = eval(constructorString);     //type function/constructor
    var evalObj = new evalNs()                //object instance

    console.log(evalNs);  //constructor
    console.log(evalObj); //the namespaced object
    console.log(ns);      //outer "ns" preserved
};​

Probably not what exactly OP was looking for but another option is to use outside variables to store values generated inside eval, as in:

var value;
var code = 'var foo = 42';
code = code.replace('var foo', 'value');
eval(code);
value // returns 42;
发布评论

评论列表(0)

  1. 暂无评论