Hướng dẫn method evaluates a string of javascript code in the context of the specified object - phương thức đánh giá một chuỗi mã javascript trong ngữ cảnh của đối tượng được chỉ định

  • Kiểm tra thực hành trực tuyến>
  • JavaScript

  • " Trước
  • Tiếp theo "

Tùy chọn - Eval - Parsedoule - Parseobject - EFLOAT
- Eval
- ParseDoule
- ParseObject
- Efloat

Câu trả lời đúng: Eval


  • " Trước
  • Tiếp theo "

Tùy chọn - Eval - Parsedoule - Parseobject - EFLOAT

Câu trả lời đúng: Eval
Viết ý kiến ​​của bạn Email must be in the form
Câu trả lời đúng: Eval
Câu trả lời đúng: Eval

Hướng dẫn method evaluates a string of javascript code in the context of the specified object - phương thức đánh giá một chuỗi mã javascript trong ngữ cảnh của đối tượng được chỉ định

Viết ý kiến ​​của bạn
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)

Cảnh báo: Thực hiện JavaScript từ một chuỗi là một rủi ro bảo mật khổng lồ. Một diễn viên xấu là quá dễ dàng để chạy mã tùy ý khi bạn sử dụng

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6. Xem không bao giờ sử dụng eval () !, Dưới đây. Executing JavaScript from a string is an enormous security risk. It is far too easy for a bad actor to run arbitrary code when you use
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6. See Never use eval()!, below.

Hàm

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 đánh giá mã JavaScript được biểu thị dưới dạng chuỗi và trả về giá trị hoàn thành của nó. Nguồn được phân tích cú pháp như một kịch bản.
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6
function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.

Thử nó

Cú pháp

Thông số

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
8

Một chuỗi đại diện cho một biểu thức, câu lệnh hoặc chuỗi câu lệnh JavaScript. Biểu thức có thể bao gồm các biến và thuộc tính của các đối tượng hiện có. Nó sẽ được phân tích cú pháp như một tập lệnh, do đó, các khai báo ____29 (chỉ có thể tồn tại trong các mô -đun) không được phép.

Giá trị trả về

Giá trị hoàn thành của việc đánh giá mã đã cho. Nếu giá trị hoàn thành trống,

function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
0 sẽ được trả về.

Sự mô tả

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 là một thuộc tính chức năng của đối tượng toàn cầu.

Đối số của hàm

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 là một chuỗi. Nó sẽ đánh giá chuỗi nguồn là một cơ thể tập lệnh, có nghĩa là cả hai câu và biểu thức đều được cho phép. Nó trả về giá trị hoàn thành của mã. Đối với các biểu thức, đó là giá trị mà biểu thức đánh giá. Nhiều tuyên bố và khai báo cũng có giá trị hoàn thành, nhưng kết quả có thể đáng ngạc nhiên (ví dụ: giá trị hoàn thành của một gán là giá trị được gán, nhưng giá trị hoàn thành của
function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
3 không được xác định), do đó, bạn nên không dựa vào các câu lệnh ' hoàn thành giá trị.

Có hai chế độ của các cuộc gọi

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6: eval trực tiếp và eval. Chỉ số trực tiếp chỉ có một dạng:
function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
5 (tên của hàm được gọi là
function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
6 và giá trị của nó là hàm
function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
6 toàn cầu). Mọi thứ khác, bao gồm cả việc gọi nó thông qua một biến bí danh, thông qua truy cập thành viên hoặc biểu thức khác hoặc thông qua toán tử
function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
8 chuỗi tùy chọn, là gián tiếp.

// Indirect call using the comma operator to return eval
(0, eval)('x + y');

// Indirect call through optional chaining
eval?.('x + y');

// Indirect call using a variable to store and return eval
const geval = eval;
geval('x + y');

// Indirect call through member access
const obj = { eval };
obj.eval('x + y');

Eval Eval có thể được nhìn thấy như thể mã được đánh giá trong một thẻ

function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
9 riêng biệt. Điều này có nghĩa là:

  • EDIRECT EVAL hoạt động trong phạm vi toàn cầu thay vì phạm vi cục bộ và mã được đánh giá không có quyền truy cập vào các biến cục bộ trong phạm vi mà nó được gọi.

    function test() {
      const x = 2, y = 4;
      // Direct call, uses local scope
      console.log(eval('x + y')); // Result is 6
      console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
    }
    

  • Gián tiếp
    function nonStrictContext() {
      eval(`with(Math) console.log(PI);`);
    }
    function strictContext() {
      "use strict";
      eval(`with(Math) console.log(PI);`);
    }
    nonStrictContext(); // Logs 3.141592653589793
    strictContext(); // Throws a SyntaxError because it's in strict mode
    
    6 sẽ không thừa hưởng sự nghiêm ngặt của bối cảnh xung quanh và sẽ chỉ ở chế độ nghiêm ngặt nếu bản thân chuỗi nguồn có chỉ thị
    // Neither context nor source string is strict,
    // so var creates a variable in the surrounding scope
    eval("var a = 1;");
    console.log(a); // 1
    // Context is not strict, but eval source is strict,
    // so b is scoped to the evaluated script
    eval("'use strict'; var b = 1;");
    console.log(b); // ReferenceError: b is not defined
    
    function strictContext() {
      "use strict";
      // Context is strict, but this is indirect and the source
      // string is not strict, so c is still global
      eval?.("var c = 1;");
      // Direct eval in a strict context, so d is scoped
      eval("var d = 1;");
    }
    strictContext();
    console.log(c); // 1
    console.log(d); // ReferenceError: d is not defined
    
    1.

    function strictContext() {
      "use strict";
      eval?.(`with(Math) console.log(PI);`);
    }
    function strictContextStrictEval() {
      "use strict";
      eval?.(`"use strict"; with(Math) console.log(PI);`);
    }
    strictContext(); // Logs 3.141592653589793
    strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
    

    Mặt khác, Eval trực tiếp thừa hưởng sự nghiêm ngặt của bối cảnh gọi.

    function nonStrictContext() {
      eval(`with(Math) console.log(PI);`);
    }
    function strictContext() {
      "use strict";
      eval(`with(Math) console.log(PI);`);
    }
    nonStrictContext(); // Logs 3.141592653589793
    strictContext(); // Throws a SyntaxError because it's in strict mode
    

  • ________ Các biến và khai báo hàm 42 được tuyên bố sẽ đi vào phạm vi xung quanh nếu chuỗi nguồn không được giải thích trong chế độ nghiêm ngặt-đối với Eval gián tiếp, chúng trở thành các biến toàn cầu. Nếu đó là một đánh giá trực tiếp trong bối cảnh chế độ nghiêm ngặt hoặc nếu bản thân chuỗi nguồn
    function nonStrictContext() {
      eval(`with(Math) console.log(PI);`);
    }
    function strictContext() {
      "use strict";
      eval(`with(Math) console.log(PI);`);
    }
    nonStrictContext(); // Logs 3.141592653589793
    strictContext(); // Throws a SyntaxError because it's in strict mode
    
    6 ở chế độ nghiêm ngặt, thì
    // Neither context nor source string is strict,
    // so var creates a variable in the surrounding scope
    eval("var a = 1;");
    console.log(a); // 1
    // Context is not strict, but eval source is strict,
    // so b is scoped to the evaluated script
    eval("'use strict'; var b = 1;");
    console.log(b); // ReferenceError: b is not defined
    
    function strictContext() {
      "use strict";
      // Context is strict, but this is indirect and the source
      // string is not strict, so c is still global
      eval?.("var c = 1;");
      // Direct eval in a strict context, so d is scoped
      eval("var d = 1;");
    }
    strictContext();
    console.log(c); // 1
    console.log(d); // ReferenceError: d is not defined
    
    2 và khai báo chức năng không "rò rỉ" vào phạm vi xung quanh.

    // Neither context nor source string is strict,
    // so var creates a variable in the surrounding scope
    eval("var a = 1;");
    console.log(a); // 1
    // Context is not strict, but eval source is strict,
    // so b is scoped to the evaluated script
    eval("'use strict'; var b = 1;");
    console.log(b); // ReferenceError: b is not defined
    
    function strictContext() {
      "use strict";
      // Context is strict, but this is indirect and the source
      // string is not strict, so c is still global
      eval?.("var c = 1;");
      // Direct eval in a strict context, so d is scoped
      eval("var d = 1;");
    }
    strictContext();
    console.log(c); // 1
    console.log(d); // ReferenceError: d is not defined
    

    Các khai báo
    function nonStrictContext() {
      eval(`with(Math) console.log(PI);`);
    }
    function strictContext() {
      "use strict";
      eval(`with(Math) console.log(PI);`);
    }
    nonStrictContext(); // Logs 3.141592653589793
    strictContext(); // Throws a SyntaxError because it's in strict mode
    
    3 và
    // Neither context nor source string is strict,
    // so var creates a variable in the surrounding scope
    eval("var a = 1;");
    console.log(a); // 1
    // Context is not strict, but eval source is strict,
    // so b is scoped to the evaluated script
    eval("'use strict'; var b = 1;");
    console.log(b); // ReferenceError: b is not defined
    
    function strictContext() {
      "use strict";
      // Context is strict, but this is indirect and the source
      // string is not strict, so c is still global
      eval?.("var c = 1;");
      // Direct eval in a strict context, so d is scoped
      eval("var d = 1;");
    }
    strictContext();
    console.log(c); // 1
    console.log(d); // ReferenceError: d is not defined
    
    6 trong chuỗi được đánh giá luôn được phân chia theo tập lệnh đó.
  • Eval trực tiếp có thể có quyền truy cập vào các biểu thức bối cảnh bổ sung. Ví dụ: trong cơ thể của một chức năng, người ta có thể sử dụng
    // Neither context nor source string is strict,
    // so var creates a variable in the surrounding scope
    eval("var a = 1;");
    console.log(a); // 1
    // Context is not strict, but eval source is strict,
    // so b is scoped to the evaluated script
    eval("'use strict'; var b = 1;");
    console.log(b); // ReferenceError: b is not defined
    
    function strictContext() {
      "use strict";
      // Context is strict, but this is indirect and the source
      // string is not strict, so c is still global
      eval?.("var c = 1;");
      // Direct eval in a strict context, so d is scoped
      eval("var d = 1;");
    }
    strictContext();
    console.log(c); // 1
    console.log(d); // ReferenceError: d is not defined
    
    7:

    function Ctor() {
      eval("console.log(new.target)");
    }
    new Ctor(); // [Function: Ctor]
    

Trong chế độ nghiêm ngặt, khai báo một biến có tên

function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
6 hoặc gán lại
function nonStrictContext() {
  eval(`with(Math) console.log(PI);`);
}
function strictContext() {
  "use strict";
  eval(`with(Math) console.log(PI);`);
}
nonStrictContext(); // Logs 3.141592653589793
strictContext(); // Throws a SyntaxError because it's in strict mode
6 là
function Ctor() {
  eval("console.log(new.target)");
}
new Ctor(); // [Function: Ctor]
0.

"use strict";

const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode

Nếu đối số của

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 không phải là một chuỗi,
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 sẽ trả lại đối số không thay đổi. Trong ví dụ sau, hàm tạo
function Ctor() {
  eval("console.log(new.target)");
}
new Ctor(); // [Function: Ctor]
3 được chỉ định và
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 trả về một đối tượng
function Ctor() {
  eval("console.log(new.target)");
}
new Ctor(); // [Function: Ctor]
3 thay vì đánh giá chuỗi.

eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4

Bạn có thể làm việc xung quanh giới hạn này một cách chung chung bằng cách sử dụng

function Ctor() {
  eval("console.log(new.target)");
}
new Ctor(); // [Function: Ctor]
6.

const expression = new String('2 + 2');
eval(expression.toString());            // returns 4

Không bao giờ sử dụng eval ()!

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 là một chức năng nguy hiểm, thực thi mã mà nó được thông qua với các đặc quyền của người gọi. Nếu bạn chạy
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 với một chuỗi có thể bị ảnh hưởng bởi một bên độc hại, bạn có thể sẽ chạy mã độc trên máy của người dùng với quyền của trang web / tiện ích mở rộng của bạn. Quan trọng hơn, mã của bên thứ ba có thể thấy phạm vi mà
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 đã được gọi (nếu đó là một đánh giá trực tiếp), điều này có thể dẫn đến các cuộc tấn công có thể theo cách mà
"use strict";

const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode
0 tương tự không dễ bị ảnh hưởng.

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 cũng chậm hơn so với các lựa chọn thay thế, vì nó phải gọi trình thông dịch JavaScript, trong khi nhiều cấu trúc khác được tối ưu hóa bởi các động cơ JS hiện đại.

Ngoài ra, các phiên dịch viên JavaScript hiện đại chuyển đổi JavaScript thành mã máy. Điều này có nghĩa là bất kỳ khái niệm đặt tên biến bị xóa sổ. Do đó, bất kỳ việc sử dụng

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 nào cũng sẽ buộc trình duyệt thực hiện tra cứu tên biến đắt tiền dài để tìm ra nơi biến tồn tại trong mã máy và đặt giá trị của nó. Ngoài ra, những điều mới có thể được giới thiệu cho biến đó thông qua
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6, chẳng hạn như thay đổi loại biến đó, buộc trình duyệt phải đánh giá lại tất cả các mã máy được tạo để bù.

May mắn thay, có một sự thay thế rất tốt cho

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6: sử dụng hàm tạo
"use strict";

const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode
0. Mã xấu với
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6:

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

Mã tốt hơn mà không cần

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6:

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
0

Hai đoạn mã ở trên dường như hoạt động theo cùng một cách, nhưng chúng không;

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 là một sự chậm hơn rất nhiều. Thông báo
"use strict";

const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode
9 trong chuỗi được đánh giá. Trong chức năng không có
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6, đối tượng đang được đánh giá trong phạm vi toàn cầu, do đó, trình duyệt an toàn cho rằng
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
1 đề cập đến
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
2 thay vì biến cục bộ gọi là
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
1. Tuy nhiên, trong mã sử dụng
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6, trình duyệt không thể giả định điều này. Ví dụ: trong mã sau,
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
1 trong chuỗi được đánh giá không đề cập đến
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
2.

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
1

Do đó, trong phiên bản

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 của mã, trình duyệt buộc phải thực hiện cuộc gọi tra cứu đắt tiền để kiểm tra xem liệu có bất kỳ biến cục bộ nào được gọi là
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
8 không. Điều này là vô cùng không hiệu quả so với
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
9.

Trong một trường hợp liên quan, nếu bạn thực sự muốn chức năng

eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
8 của mình được gọi từ mã bên trong
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
9, bạn có nên dễ dàng thoát ra và sử dụng
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 không? Không! Không bao giờ. Thay vào đó, hãy thử cách tiếp cận dưới đây.

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
2

Mã ở trên có vẻ không hiệu quả chậm vì hàm tăng gấp ba, nhưng hãy phân tích lợi ích của phương pháp hiệu quả ở trên:

  • Nó cho phép mã trong chuỗi được chuyển đến
    const expression = new String('2 + 2');
    eval(expression.toString());            // returns 4
    
    3 được thu nhỏ.
  • Chi phí gọi chức năng là tối thiểu, làm cho kích thước mã nhỏ hơn nhiều giá trị lợi ích
  • eval(new String('2 + 2')); // returns a String object containing "2 + 2"
    eval('2 + 2');             // returns 4
    
    9 dễ dàng hơn cho phép mã của bạn được hưởng lợi từ các cải tiến hiệu suất có thể có bởi
    const expression = new String('2 + 2');
    eval(expression.toString());            // returns 4
    
    5
  • Mã không sử dụng
    function strictContext() {
      "use strict";
      eval?.(`with(Math) console.log(PI);`);
    }
    function strictContextStrictEval() {
      "use strict";
      eval?.(`"use strict"; with(Math) console.log(PI);`);
    }
    strictContext(); // Logs 3.141592653589793
    strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
    
    6, làm cho nó đơn hàng có độ lớn nhanh hơn so với cách khác.

Cuối cùng, hãy kiểm tra Minification. Với việc sử dụng

eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
9 như được hiển thị ở trên, bạn có thể thu nhỏ chuỗi mã được chuyển đến
const expression = new String('2 + 2');
eval(expression.toString());            // returns 4
3 hiệu quả hơn nhiều vì các tên đối số chức năng có thể được thu nhỏ như trong mã được thu nhỏ bên dưới.

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
3

Ngoài ra còn có các lựa chọn thay thế an toàn hơn (và nhanh hơn!) Cho

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 hoặc
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
9 cho các trường hợp sử dụng phổ biến.

Sự khác biệt giữa

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 và
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
9 là chuỗi nguồn được chuyển đến
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2');             // returns 4
9 được phân tích cú pháp như cơ thể chức năng, không phải là một tập lệnh. Có một vài sắc thái - ví dụ, bạn có thể sử dụng các câu lệnh
function looseJsonParse(obj) {
  return eval(`(${obj})`);
}
console.log(looseJsonParse(
  "{a:(4-1), b:function(){}, c:new Date()}"
))
4 trong một cơ thể chức năng nhưng không phải trong một kịch bản. Trong trường hợp bạn có ý định phân tích nội dung dưới dạng tập lệnh, sử dụng EDIRECT EVAL và Buộc chế độ nghiêm ngặt có thể là một giải pháp thay thế an toàn khác.

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
4

Bằng cách này, mã được đánh giá không có quyền truy cập vào phạm vi cục bộ và không thể xác định các biến toàn cầu.

Truy cập thuộc tính thành viên

Bạn không nên sử dụng

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 để chuyển đổi tên thuộc tính thành thuộc tính. Hãy xem xét ví dụ sau đây không được biết đến thuộc tính của đối tượng không được truy cập cho đến khi mã được thực thi. Điều này có thể được thực hiện với
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6:

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
5

Tuy nhiên,

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 là không cần thiết ở đây. Trong thực tế, việc sử dụng nó ở đây là không được khuyến khích. Thay vào đó, hãy sử dụng các truy cập tài sản, nhanh hơn và an toàn hơn nhiều:

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
6

Bạn thậm chí có thể sử dụng phương pháp này để truy cập các thuộc tính hậu duệ. Sử dụng

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 Điều này trông giống như:

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
7

Tránh

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 ở đây có thể được thực hiện bằng cách phân tách đường dẫn thuộc tính và lặp qua các thuộc tính khác nhau:

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
8

Đặt một thuộc tính theo cách đó hoạt động tương tự:

function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
9

Sử dụng các chức năng thay vì đánh giá các đoạn mã

JavaScript có các chức năng hạng nhất, có nghĩa là bạn có thể chuyển các hàm dưới dạng đối số cho các API khác, lưu trữ chúng trong các thuộc tính của biến và đối tượng, v.v. Nhiều API DOM được thiết kế với tâm trí này, vì vậy bạn có thể (và nên) viết:

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
0

Việc đóng cũng hữu ích như một cách để tạo các hàm được tham số hóa mà không cần nối các chuỗi.

Phân tích cú pháp JSON (chuyển đổi chuỗi thành các đối tượng JavaScript)

Nếu chuỗi bạn đang gọi

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 trên có chứa dữ liệu (ví dụ: một mảng:
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
01), trái ngược với mã, bạn nên xem xét chuyển sang JSON, cho phép chuỗi sử dụng một tập hợp con của cú pháp JavaScript để thể hiện dữ liệu.

Lưu ý rằng vì cú pháp JSON bị hạn chế so với cú pháp JavaScript, nên nhiều chữ JavaScript hợp lệ sẽ không phân tích thành JSON. Ví dụ, dấu phẩy kéo dài không được phép trong JSON và tên tài sản (khóa) trong nghĩa đen đối tượng phải được đặt trong các trích dẫn. Hãy chắc chắn sử dụng trình tự kiểm tra JSON để tạo các chuỗi mà sau này sẽ được phân tích cú pháp dưới dạng JSON.

Truyền dữ liệu thay vì mã

Ví dụ: một tiện ích mở rộng được thiết kế để cạo nội dung của các trang web có thể có các quy tắc cạo được xác định trong XPath thay vì mã JavaScript.

Ví dụ

Sử dụng eval ()

Trong mã sau, cả hai câu lệnh có chứa

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 trả về 42. Đầu tiên đánh giá chuỗi
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
03; Thứ hai đánh giá chuỗi
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
04.

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
1

Eval () Trả về giá trị hoàn thành của các câu lệnh

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 Trả về giá trị hoàn thành của các câu lệnh. Đối với
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
06, nó sẽ là biểu thức hoặc tuyên bố cuối cùng được đánh giá.

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
2

Ví dụ sau sử dụng

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 để đánh giá chuỗi
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
08. Chuỗi này bao gồm các câu lệnh JavaScript gán
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
09 giá trị 42 nếu
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
10 là năm và gán 0 cho
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
09 nếu không. Khi câu lệnh thứ hai được thực thi,
function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
6 sẽ khiến các câu lệnh này được thực hiện và nó cũng sẽ đánh giá tập hợp các câu lệnh và trả về giá trị được gán cho
function test() {
  const x = 2, y = 4;
  // Direct call, uses local scope
  console.log(eval('x + y')); // Result is 6
  console.log(eval?.('x + y')); // Uses global scope, throws because x is undefined
}
09, vì giá trị hoàn thành của một gán là giá trị được gán.

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
3

Nếu bạn gán nhiều giá trị thì giá trị cuối cùng được trả về.

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
4

eval () dưới dạng hàm xác định chuỗi yêu cầu "(" và ")" như tiền tố và hậu tố

function strictContext() {
  "use strict";
  eval?.(`with(Math) console.log(PI);`);
}
function strictContextStrictEval() {
  "use strict";
  eval?.(`"use strict"; with(Math) console.log(PI);`);
}
strictContext(); // Logs 3.141592653589793
strictContextStrictEval(); // Throws a SyntaxError because the source string is in strict mode
5

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # Sec-Eval-X
# sec-eval-x

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm thế nào để bạn đánh giá một chuỗi trong JavaScript?

Eval () là một thuộc tính chức năng của đối tượng toàn cầu. Đối số của hàm eval () là một chuỗi. Nó sẽ đánh giá chuỗi nguồn là một cơ thể tập lệnh, có nghĩa là cả hai câu và biểu thức đều được cho phép. Nó trả về giá trị hoàn thành của mã. is a function property of the global object. The argument of the eval() function is a string. It will evaluate the source string as a script body, which means both statements and expressions are allowed. It returns the completion value of the code.

Làm thế nào để bạn đánh giá một mã JavaScript?

Hàm eval () trong javascript được sử dụng để đánh giá biểu thức.Đây là chức năng toàn cầu của Javascirpt, đánh giá chuỗi được chỉ định là mã JavaScript và thực thi nó.Tham số của hàm eval () là một chuỗi.Nếu tham số đại diện cho các câu lệnh, eval () sẽ đánh giá các câu lệnh. in JavaScript is used to evaluate the expression. It is JavaScirpt's global function, which evaluates the specified string as JavaScript code and executes it. The parameter of the eval() function is a string. If the parameter represents the statements, eval() evaluates the statements.

Eval () làm gì trong JavaScript?

JavaScript eval () Phương thức Eval () đánh giá hoặc thực thi một đối số.Nếu đối số là một biểu thức, eval () đánh giá biểu thức.Nếu đối số là một hoặc nhiều câu lệnh javascript, eval () sẽ thực thi các câu lệnh.

Tại sao eval () là cái ác?

Eval () là xấu nếu chạy trên máy chủ bằng cách sử dụng đầu vào được gửi bởi một máy khách không được tạo bởi nhà phát triển hoặc không được nhà phát triển vệ sinh.Eval () không xấu nếu chạy trên máy khách, ngay cả khi sử dụng đầu vào không được trình bày do khách hàng tạo ra.if running on the server using input submitted by a client that was not created by the developer or that was not sanitized by the developer. eval() is not evil if running on the client, even if using unsanitized input crafted by the client.