What are the differences between these two methods of executing codewhich is in a string?

Discussion in 'Javascript' started by Laser Lips, Feb 24, 2011.

  1. Laser Lips

    Laser Lips Guest

    What are the differences between these two methods of executing code
    which is in a string?

    <script type='text/javascript'>
    var d="I'm d";
    var h="alert(d);";
    //method 1
    eval(h);
    //method 2
    (new Function(h))();
    </script>

    Graham
     
    Laser Lips, Feb 24, 2011
    #1
    1. Advertising

  2. Re: What are the differences between these two methods of executingcode which is in a string?

    Laser Lips wrote:
    > What are the differences between these two methods of executing code
    > which is in a string?


    In particular code the main difference cannot be observed, because you
    have defined `d' in global execution context. Here the difference is
    in the Scope Chain of execution contexts created by eval code and by
    execution of the function object created by Function constructor.
    Execution context for eval code uses caller's Variable Object for its
    VO during variable instantiation. Functions created by Function
    constructor never form closure. The internal [[Scope]] property always
    refers Global Object and Global Object is used for VO in global
    execution context. This is the reason that you cannot observe the main
    difference.


    > <script type='text/javascript'>
    >     var d="I'm d";
    >     var h="alert(d);";
    >     //method 1
    >     eval(h);


    The Scope Chain for eval execution context contains:

    Global Object

    >     //method 2
    >     (new Function(h))();


    The Scope Chain here is:

    GlobalObject
    ^
    VariableObject of execution context for function code
    > </script>


    If you would like to see the difference try out:

    (function () {
    var d="I'm d";
    var h="alert(d);";

    eval(h); //OK

    (new Function(h))(); //ReferenceError
    })();
     
    Asen Bozhilov, Feb 24, 2011
    #2
    1. Advertising

  3. Laser Lips

    Laser Lips Guest

    Re: What are the differences between these two methods of executingcode which is in a string?

    On Feb 24, 10:36 am, Asen Bozhilov <> wrote:
    > Laser Lips wrote:
    > > What are the differences between these two methods of executing code
    > > which is in a string?

    >
    > In particular code the main difference cannot be observed, because you
    > have defined `d' in global execution context. Here the difference is
    > in the Scope Chain of execution contexts created by eval code and by
    > execution of the function object created by Function constructor.
    > Execution context for eval code uses caller's Variable Object for its
    > VO during variable instantiation. Functions created by Function
    > constructor never form closure. The internal [[Scope]] property always
    > refers Global Object and Global Object is used for VO in global
    > execution context. This is the reason that you cannot observe the main
    > difference.
    >
    > > <script type='text/javascript'>
    > >     var d="I'm d";
    > >     var h="alert(d);";
    > >     //method 1
    > >     eval(h);

    >
    > The Scope Chain for eval execution context contains:
    >
    > Global Object
    >
    > >     //method 2
    > >     (new Function(h))();

    >
    > The Scope Chain here is:
    >
    > GlobalObject
    >      ^
    > VariableObject of execution context for function code
    >
    > > </script>

    >
    > If you would like to see the difference try out:
    >
    > (function () {
    >    var d="I'm d";
    >    var h="alert(d);";
    >
    >    eval(h); //OK
    >
    >    (new Function(h))(); //ReferenceError
    >
    > })();
    >
    >


    Thanks Guys.
    Graham
     
    Laser Lips, Feb 24, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John_Woo
    Replies:
    5
    Views:
    1,529
  2. JohnJSal
    Replies:
    13
    Views:
    440
    John Salerno
    Nov 4, 2006
  3. xz
    Replies:
    4
    Views:
    295
    James Kanze
    Mar 16, 2008
  4. dpapathanasiou
    Replies:
    3
    Views:
    297
    Lie Ryan
    Dec 8, 2009
  5. iMath
    Replies:
    5
    Views:
    251
    Piet van Oostrum
    Sep 9, 2012
Loading...

Share This Page