[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval [Back]  [Original]

eval() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

eval()

20157

eval() JavaScript

console.log(eval("2 + 2"));
// Expected output: 4

console.log(eval(new String("2 + 2")));
// Expected output: 2 + 2

console.log(eval("2 + 2") === eval("4"));
// Expected output: true

console.log(eval("2 + 2") === eval(new String("2 + 2")));
// Expected output: false

eval(string)

string

JavaScript

undefined

eval()

eval() eval() JavaScript eval() eval() JavaScript

eval() x 3 * x + 2 eval() x

eval() eval() String eval() String

js
eval(new String("2 + 2")); // "2 + 2"
eval("2 + 2"); // returns 4

toString()

js
var expression = new String("2 + 2");
eval(expression.toString());

eval() eval ECMAScript 5 eval

js
function test() {
  var x = 2,
    y = 4;
  console.log(eval("x + y")); //  6
  var geval = eval; // 
  console.log(geval("x + y")); // throws ReferenceError  `x` 
  (0, eval)("x + y"); // 
}

eval

eval() eval() / eval() Function

eval() JS JS

JavaScript JavaScript eval eval() eval window.Functioneval()Function()

eval

js
function looseJsonParse(obj) {
  return eval("(" + obj + ")");
}
console.log(looseJsonParse("{a:(4-1), b:function(){}, c:new Date()}"));

eval

js
function looseJsonParse(obj) {
  return Function('"use strict";return (' + obj + ")")();
}
console.log(looseJsonParse("{a:(4-1), b:function(){}, c:new Date()}"));

eval c: new Date() eval Date window.Date Date eval()

js
function Date(n) {
  return [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
  ][n % 7 || 0];
}
function looseJsonParse(obj) {
  return eval("(" + obj + ")");
}
console.log(looseJsonParse("{a:(4-1), b:function(){}, c:new Date()}"));

eval() Date() Function()

Function() Date eval()

js
function Date(n) {
  return [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
  ][n % 7 || 0];
}
function runCodeWithDateFunction(obj) {
  return Function('"use strict";return (' + obj + ")")()(Date);
}
console.log(runCodeWithDateFunction("function(Date){ return Date(5) }"));

  1. runCodeWithDateFunction

  2. Function() "use strict"

  3. eval()

Function()runCodeWithDateFunction

js
console.log(
  Function('"use strict";return(function(a){return a(5)})')()(function (a) {
    return "Monday Tuesday Wednesday Thursday Friday Saturday Sunday".split(
      " ",
    )[a % 7 || 0];
  }),
);

eval()Function() ()

eval() eval

js
var obj = { a: 20, b: 30 };
var propName = getPropName(); //  "a"  "b"

eval("var result = obj." + propsName);

eval()

js
var obj = { a: 20, b: 30 };
var propName = getPropName(); //  "a"  "b"
var result = obj[propName]; // obj[ "a" ]  obj.a 

js
var obj = { a: { b: { c: 0 } } };
var propPath = getPropPath(); //  "a.b.c"

eval("var result = obj." + propPath);

eval()

js
function getDescendantProp(obj, desc) {
  var arr = desc.split(".");
  while (arr.length) {
    obj = obj[arr.shift()];
  }
  return obj;
}

var obj = { a: { b: { c: 0 } } };
var propPath = getPropPath(); //  "a.b.c"
var result = getDescendantProp(obj, propPath);

js
function setDescendantProp(obj, desc, value) {
  var arr = desc.split(".");
  while (arr.length > 1) {
    obj = obj[arr.shift()];
  }
  return (obj[arr[0]] = value);
}

var obj = { a: { b: { c: 0 } } };
var propPath = getPropPath(); //  "a.b.c"
var result = setDescendantProp(obj, propPath, 1); // a.b.c  1

JavaScript first-class functions DOM API

js
//  setTimeout(" ... ", 1000) 
setTimeout(function() { ... }, 1000);

//  elt.setAttribute("onclick", "...") 
elt.addEventListener('click', function() { ... } , false);

JSON JavaScript

eval() [1,2,3] JSON JavaScript

JSON JavaScript JavaScript JSON JSON JSON JSON JSON

XPath JavaScript

XUL

eval

eval() 42 "x + y + 1" "42"

js
var x = 2;
var y = 39;
var z = "42";
eval("x + y + 1"); // returns 42
eval(z); // returns 42

eval JavaScript

eval() str x 5 Alert z 42 z 0 JavaScript eval() z 42

js
var x = 5;
var str = "if (x == 5) {console.log('z is 42'); z = 42;} else z = 0;";

console.log("z is ", eval(str));

js
var x = 5;
var str = "if (x == 5) {console.log('z is 42'); z = 42; x = 420; } else z = 0;";

console.log("x is ", eval(str)); // z is 42  x is 420

eval

js
var str = "if ( a ) { 1 + 1; } else { 1 + 2; }";
var a = true;
var b = eval(str); // returns 2

console.log("b is : " + b);

a = false;
b = eval(str); // returns 3

console.log("b is : " + b);

eval

js
var fctStr1 = "function a() {}";
var fctStr2 = "(function a() {})";
var fct1 = eval(fctStr1); //  undefined
var fct2 = eval(fctStr2); // 

ECMAScript 2027 LanguageSpecification
# sec-eval-x

Firefox


Web Proxy Viewer  |  New URL  |  Original Page