| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Strict_mode | [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.
ECMAScript 5 JavaScript JavaScript " (sloppy mode)" . , . , . - , .
JavaScript .
JavaScript , (transitioning to strict mode) .
. , {} . . eval , Function , , WindowTimers.setTimeout() .
, "use strict";( 'use strict';) .
//
"use strict";
var v = "Hi! I'm a strict mode script!";
, . , .
, strict mode , . "use strict"; ( 'use strict';).
function strict() {
// - strict mode
"use strict";
function nested() {
return "And so am I!";
}
return "Hi! I'm a strict mode function! " + nested();
}
function notStrict() {
return "I'm not strict.";
}
ECMAScript 2015 JavaScript . , 3 . JavaScript .
function strict() {
//
}
export default strict;
.
: ( ), , eval arguments ," " , ECMAScript .
. , . , . .
, . JavaScript "" ( : modern ) . .
"use strict";
// mistypedVariable
mistypedVaraible = 17; // ReferenceError
, . , NaN . NaN . . NaN . ( , getter-only , ) .
"use strict";
//
var undefined = 5; // TypeError
var Infinity = 5; // TypeError
//
var obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // TypeError
// getter-only
var obj2 = {
get x() {
return 17;
},
};
obj2.x = 5; // TypeError
//
var fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = "ohai"; // TypeError
, ( ).
"use strict";
delete Object.prototype; // TypeError
, Gecko 34 . . . .
: ECMAScript 2015 . (Firefox bug 1041128).
"use strict";
var o = { p: 1, p: 2 }; // !!!
, . . arguments[i] , . , ( ). .
function sum(a, a, c) {
// !!!
"use strict";
return a + b + c; //
}
, ECMAScript 5 8 . 8 ES5 , 0 (0644 === 420 "\045" === "%"). ECMAScript 2015 "0o" 8 .
var a = 0o10; // ES6: 8
0 , ! 8 .
"use strict";
var sum =
015 + // !!!
197 +
142;
, ECMAScript 6 primitive . (no-op), TypeError .
(function () {
"use strict";
false.true = ""; // TypeError
(14).sailing = "home"; // TypeError
"with".you = "far away"; // TypeError
})();
. X . JavaScript . JavaScript . .
, with . with ( ) . . with , with .
"use strict";
var x = 17;
with (obj) {
// !!!
// , var x ,
// obj.x ?
// ,
// .
x;
}
, with .
, eval . eval("var x;") x . , eval (eval ). eval evaluated , .
var x = 17;
var evalX = eval("'use strict'; var x = 42; x");
console.assert(x === 17);
console.assert(evalX === 42);
, eval eval(...) , evaluated . , .
function strict1(str) {
"use strict";
return eval(str); // str
}
function strict2(f, str) {
"use strict";
return f(str); // eval(...) :
// str
}
function nonstrict(str) {
return eval(str); // str
}
strict1("' !'");
strict1("'use strict'; ' !'");
strict2(eval, "' .'");
strict2(eval, "'use strict'; ' !'");
nonstrict("' .'");
nonstrict("'use strict'; ' !'");
eval eval evaluated .
, . delete name .
"use strict";
var x;
delete x; // !!!
eval("var y; delete y;"); // !!! syntax error
eval arguments arguments eval . : eval, arguments. ECMAScript eval arguments .
, eval arguments . .
"use strict";
eval = 17;
arguments++;
++eval;
var obj = { set p(arguments) {} };
var eval;
try {
} catch (arguments) {}
function x(eval) {}
function arguments() {}
var y = function eval() {};
var f = new Function("arguments", "'use strict'; return 17;");
, arguments . arg arg arguments[0] , ( , arguments[0] ). arguments . arguments[i] , arguments[i] .
function f(a) {
"use strict";
a = 42;
return [a, arguments[0]];
}
var pair = f(17);
console.assert(pair[0] === 42);
console.assert(pair[1] === 17);
, arguments.callee . arguments.callee . . . , arguments.callee , arguments.callee . arguments.callee , .
"use strict";
var f = function () {
return arguments.callee;
};
f(); // TypeError
"" JavaScript . JavaScript . JavaScript , JavaScript . JavaScript . . JavaScript .
, this (a.k.a. "boxed"). , this : this ; , this Boxed ; undefined null this . ( this call, apply, bind ) . JavaScript "" . , this , this undefined :
"use strict";
function fun() {
return this;
}
console.assert(fun() === undefined);
console.assert(fun.call(2) === 2);
console.assert(fun.apply(null) === null);
console.assert(fun.call(undefined) === undefined);
console.assert(fun.bind(true)() === true);
, window this .
, ECMAScript JavaScript "" . , fun , fun.caller fun fun.arguments fun . "" ( ) JavaScript "" . fun , both fun.caller fun.arguments .
function restricted() {
"use strict";
restricted.caller; // throws a TypeError
restricted.arguments; // throws a TypeError
}
function privilegedInvoker() {
return restricted();
}
privilegedInvoker();
, . ECMAScript arguments.caller . ; . . , arguments.caller :
"use strict";
function fun(a, b) {
"use strict";
var v = 12;
return arguments.caller; //TypeError .
}
fun(1, 2); // doesn't expose v (or a or b)
ECMAScript , ECMAScript5 . , .
, . implements, interface, let, package, private, protected, public, static, yield. , , .
function package(protected) {
// !!!
"use strict";
var implements; // !!!
// !!!
interface: while (true) {
break interface; // !!!
}
function private() {} // !!!
}
function fun(static) {
"use strict";
} // !!!
Mozilla : , JavaScript 1.7 ( , <script type=""> ) , let yield . , <script src=""> <script>...</script> , let/yield . , ES5 class, enum, export, extends, import, and super , Firefox 5 Mozilla .
, . (strict mode prohibits function statements not at the top level of a script or function). "" . ES5 !( ES3 .) . ECMAScript , , . (Prohibiting such function statements in strict mode) ECMAScript "". :
"use strict";
if (true) {
function f() {} // !!! syntax error
f();
}
for (var i = 0; i < 5; i++) {
function f2() {} // !!! syntax error
f2();
}
function baz() {
// kosher
function eit() {} // also kosher
}
, ECMAScript5 . ECMAScript , .
. , (Browser versions used in the wild that only have partial support for strict mode), , . ( , Internet Explorer 10 !) . . , . , .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification |
This page was last modified on 2024 12 17 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 |