| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/eval | [Back] [Original] |
Get to know MDN better
console.log(eval("2 + 2"));
// : 4
console.log(eval(new String("2 + 2")));
// : 2 + 2
console.log(eval("2 + 2") === eval("4"));
// : true
console.log(eval("2 + 2") === eval(new String("2 + 2")));
// : false
eval(script)
undefined script eval()
script SyntaxError
eval()
eval()
let undefined
eval eval SyntaxError
"use strict";
const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode
eval() eval() String eval() String
eval(new String("2 + 2")); // "2 + 2" String
eval("2 + 2"); // 4
const expression = new String("2 + 2");
eval(String(expression)); // 4
eval() eval eval 2 eval eval eval(...) ?.
//
eval("x + y");
// eval
(0, eval)("x + y");
//
eval?.("x + y");
// eval
const myEval = eval;
myEval("x + y");
//
const obj = { eval };
obj.eval("x + y");
eval <script>
eval
function test() {
const x = 2;
const y = 4;
//
console.log(eval("x + y")); // 6
//
console.log(eval?.("x + y")); // x
}
function nonStrictContext() {
eval?.(`with (Math) console.log(PI);`);
}
function strictContext() {
"use strict";
eval?.(`with (Math) console.log(PI);`);
}
function strictContextStrictEval() {
"use strict";
eval?.(`"use strict"; with (Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Uncaught SyntaxError: Strict mode code may not include a with statement
eval
function nonStrictContext() {
eval(`with (Math) console.log(PI);`);
}
function strictContext() {
"use strict";
eval(`with (Math) console.log(PI);`);
}
function strictContextStrictEval() {
"use strict";
eval(`"use strict"; with (Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Uncaught SyntaxError: Strict mode code may not include a with statement
strictContextStrictEval(); // Uncaught SyntaxError: Strict mode code may not include a with statement
// var
eval("var a = 1;");
console.log(a); // 1
// eval b
eval("'use strict'; var b = 1;");
console.log(b); // ReferenceError: b is not defined
function strictContext() {
"use strict";
//
eval?.("var c = 1;");
// eval d
eval("var d = 1;");
}
strictContext();
console.log(c); // 1
console.log(d); // ReferenceError: d is not defined
eval new.target
function Ctor() {
eval("console.log(new.target)");
}
new Ctor(); // [Function: Ctor]
eval()
eval() eval() eval() eval eval() eval() eval() eval() eval() eval()
function looseJsonParse(obj) {
return eval(`(${obj})`);
}
console.log(looseJsonParse("{ a: 4 - 1, b: function () {}, c: new Map() }"));
eval
function looseJsonParse(obj) {
return eval?.(`"use strict";(${obj})`);
}
console.log(looseJsonParse("{ a: 4 - 1, b: function () {}, c: new Map() }"));
2 eval
c: new Map() eval Map Map Map() eval Map window.Map()
function looseJsonParse(obj) {
class Map {}
return eval(`(${obj})`);
}
console.log(looseJsonParse(`{ a: 4 - 1, b: function () {}, c: new Map() }`));
eval() Map()
eval() var
eval
eval eval eval()
eval() Function()
Function() eval JavaScript eval()
eval() Function()Function() return
Function() eval
function add(a, b) {
return a + b;
}
function runCodeWithAddFunction(obj) {
return Function("add", `"use strict";return (${obj});`)(add);
}
console.log(runCodeWithAddFunction("add(5, 7)")); // 12
eval() Function() CSP eval() Function()
eval() eval()
const obj = { a: 20, b: 30 };
const propName = getPropName(); // returns "a" or "b"
const result = eval(`obj.${propName}`);
const obj = { a: 20, b: 30 };
const propName = getPropName(); // "a" "b"
const result = obj[propName]; // obj[ "a" ] obj.a
eval()
const obj = { a: { b: { c: 0 } } };
const propPath = getPropPath(); // "a.b.c"
const result = eval(`obj.${propPath}`); // 0
eval()
function getDescendantProp(obj, desc) {
const arr = desc.split(".");
while (arr.length) {
obj = obj[arr.shift()];
}
return obj;
}
const obj = { a: { b: { c: 0 } } };
const propPath = getPropPath(); // "a.b.c"
const result = getDescendantProp(obj, propPath);
function setDescendantProp(obj, desc, value) {
const arr = desc.split(".");
while (arr.length > 1) {
obj = obj[arr.shift()];
}
return (obj[arr[0]] = value);
}
const obj = { a: { b: { c: 0 } } };
const propPath = getPropPath(); // "a.b.c"
const result = setDescendantProp(obj, propPath, 1); // obj.a.b.c 1
// setTimeout(" ... ", 1000)
setTimeout(() => {
//
}, 1000);
// elt.setAttribute("onclick", "")
elt.addEventListener("click", () => {
//
});
eval() "[1, 2, 3]" JSON JavaScript
JSON JavaScript JavaScript JSON JSON JSON JSON
JavaScript XPath
eval() 42 "x + y + 1" 2 "42"
const x = 2;
const y = 39;
const z = "42";
eval("x + y + 1"); // 42
eval(z); // 42
eval() if
const str = "if (a) { 1 + 1 } else { 1 + 2 }";
let a = true;
let b = eval(str);
console.log(`b is: ${b}`); // b is: 2
a = false;
b = eval(str);
console.log(`b is: ${b}`); // b is: 3
eval() str x 5 z 42 z 0 JavaScript 2 eval() z
const x = 5;
const str = `if (x === 5) {
console.log("z is 42");
z = 42;
} else {
z = 0;
}`;
console.log("z is ", eval(str)); // z is 42 z is 42
let x = 5;
const 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 "(" ")" //
const fctStr1 = "function a() {}";
//
const fctStr2 = "(function b() {})";
const fct1 = eval(fctStr1); // undefined `a`
const fct2 = eval(fctStr2); // `b`
| ECMAScript 2027 LanguageSpecification # sec-eval-x |
| Web Proxy Viewer | New URL | Original Page |