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

L

Laser Lips

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
 
A

Asen Bozhilov

Laser said:
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
})();
 
L

Laser Lips

Laser said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top