eval

by owen_pengtao

JavaScript

document.write("<pre>");

document.writeln(typeof un);        // "undefined"
document.writeln(typeof deux);      // "undefined"
document.writeln(typeof trois);     // "undefined"

var jsstring = "var un = 1; document.writeln(un);";
eval(jsstring);                    // logs "1"

jsstring = "var deux = 2; document.writeln(deux);";
new Function(jsstring)();         // logs "2"

jsstring = "var trois = 3; document.writeln(trois);";
(function () {
   eval(jsstring);
}());                             // logs "3"

document.writeln(typeof un);      // number
document.writeln(typeof deux);    // "undefined"
document.writeln(typeof trois);   // "undefined"


document.write("</pre>");