| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/eval | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
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 .
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);
stringJavaScript , , . .
. undefined .
eval() .
eval() . eval() . JavaScript . eval() . JavaScript .
eval() . x x , "3 * x + 2" eval() .
eval() eval() . , String String .
eval(new String("2 + 2")); // "2 + 2" String
eval("2 + 2"); // 4
toString() .
var expression = new String("2 + 2");
eval(expression.toString()); // 4
eval ECMAScript 5 . eval() , .
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 :
function looseJsonParse(obj) {
return eval(obj);
}
console.log(looseJsonParse("{a:(4-1), b:function(){}, c:new Date()}"));
eval :
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 .
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() ? . .
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 .
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 .
var obj = { a: 20, b: 30 };
var propname = getPropName(); // "a" "b"
eval("var result = obj." + propname);
var obj = { a: 20, b: 30 };
var propname = getPropName(); // "a" "b"
var result = obj[propname]; // obj[ "a" ] obj.a
. eval() .
var obj = { a: { b: { c: 0 } } };
var propPath = getPropPath(); // "a.b.c"
eval("var result = obj." + propPath);
eval() split .
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);
.
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 , .
// setTimeout(" ... ", 1000)
setTimeout(function() { ... }, 1000);
// elt.setAttribute("onclick", "...")
elt.addEventListener("click", function() { ... } , false);
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" .
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 .
var x = 5;
var str = "if (x == 5) {console.log('z is 42'); z = 42;} else z = 0; ";
console.log("z is ", eval(str));
.
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() .
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 "(" ")" var fctStr1 = "function a() {}";
var fctStr2 = "(function a() {})";
var fct1 = eval(fctStr1); // undefined
var fct2 = eval(fctStr2); //
This page was last modified on 2026 7 15 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |