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

eval() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

eval()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

: : eval() . eval() . eval ! .

eval() JavaScript .

In this article

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

js
eval(string);

string

JavaScript , , . .

. undefined .

eval() .

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

eval() . x x , "3 * x + 2" eval() .

eval() eval() . , String String .

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

toString() .

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

eval ECMAScript 5 . eval() , .

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

eval !

eval() caller . eval() , . , 3 eval() , Function .

JS eval() JS .

, JavaScript JavaScript . , . eval . eval() , . , () window.Function eval . eval() 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 . eval() Date .

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() .

Date Function() . 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 call overhead is minimal, making the far smaller code size well worth the benefit

3. Function() "use strict"; .

4. 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." + propname);

eval() , . .

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

. eval() .

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

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

eval() split .

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); // test.a.b.c  1 

JavaScript 1 API , . DOM API , .

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

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

.

JSON ( JavaScript )

eval() ( "[1, 2, 3]" ), JavaScript JSON . Downloading JSON and JavaScript in extensions .

JSON JavaScript , JavaScript JSON . , JSON , () . JSON JSON .

, JavaScript XPath .

3 . XUL , Components.utils.evalInSandbox .

eval

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

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

eval JavaScript

eval() str . x 5 z 42 , z 0 JavaScript . , eval() , z .

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 42, x 420

eval() .

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

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

a = false;
b = eval(str); // 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); //  


Web Proxy Viewer  |  New URL  |  Original Page