SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get all variables in current scope

    Is there a javascript equivalent to PHP's get_defined_vars()?

    I'm overriding eval() with my own function like so:
    Code JavaScript:
    real_eval = eval;
    eval = function(str) {
       // Do stuff to str first
       return real_eval(str);
    }

    But any variables that should get initialised into the current scope by the eval() get stuck inside the eval wrapper function.

    Code JavaScript:
    function test() {
     
        var myVar;
        eval('myVar = "my value"');
        return myVar;
     
    }
     
    alert(test());

    So that this returns "undefined". I was thinking I could get all the defined vars after the new eval() calls real_eval() and make them into globals. It's not a very elegant solution but I couldn't think of anything else. I have to edit the string being eval'ed and I have no control over what gets passed to eval() or how.

  2. #2
    Non-Member
    Join Date
    Jul 2005
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you want to use eval anyway

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bob Carologees View Post
    Why would you want to use eval anyway
    and

    http://developer.mozilla.org/En/Core...Functions/Eval
    Don't use eval!
    eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension.

    There are safe alternatives to eval() for common use-cases.

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies but the question was

    Is there a javascript equivalent to PHP's get_defined_vars()?
    I'm aware of the advantages and disadvantages of using eval and was not asking for comments on the context, only the question.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •