Skip to content

Strict 模式

一句话解释

Strict 模式 是 opt-in 模式 那个 制作 JavaScript stricter, catching 常见错误 as 错误 相反 的 silent failures.

用大白话说

Strict 模式 ('use strict') is a way to opt into a restricted variant of JavaScript that eliminates some silent errors, fixes mistakes that make optimization difficult, and prohibits some unsafe syntax.

In strict mode: assigning to undeclared variables throws an error (instead of creating a global), 这个 is undefined in regular functions (instead of the global object), duplicate parameter names are not allowed, and deleting non-deletable properties throws.

To enable strict mode for a whole file, put 'use strict'; at the top. For a single function, put it inside the function body. ES modules and class bodies are always in strict mode automatically. Using strict mode is recommended for all new code.

示例

javascript
"use strict";
undeclaredVar = 10; // ReferenceError

function fn() {
  "use strict";
  return this; // undefined (not global)
}

Strict 模式 catches undeclared 变量 and 更改 这个 behavior.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表