[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Strict_mode [Back]  [Original]

Strict mode - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Strict mode

" (sloppy mode)" . .

ECMAScript 5 JavaScript JavaScript " (sloppy mode)" . , . , . - , .

JavaScript .

  1. throwing.
  2. JavaScript . - .
  3. ECMAScript .

JavaScript , (transitioning to strict mode) .

In this article

. , {} . . eval , Function , , WindowTimers.setTimeout() .

, "use strict";( 'use strict';) .

js
//     
"use strict";
var v = "Hi!  I'm a strict mode script!";

. . - . ! . , - . , , .

, . , .

strict mode

, strict mode , . "use strict"; ( 'use strict';).

js
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.";
}

strict mode

ECMAScript 2015 JavaScript . , 3 . JavaScript .

js
function strict() {
  //   
}
export default strict;

. : ( ), , eval arguments ," " , ECMAScript .

. , . , . .

, . JavaScript "" ( : modern ) . .

js
"use strict";
//   mistypedVariable   
mistypedVaraible = 17; //     ReferenceError  

, . , NaN . NaN . . NaN . ( , getter-only , ) .

js
"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 

, ( ).

js
"use strict";
delete Object.prototype; // TypeError 

, Gecko 34 . . . .

: ECMAScript 2015 . (Firefox bug 1041128).

js
"use strict";
var o = { p: 1, p: 2 }; // !!!  

, . . arguments[i] , . , ( ). .

js
function sum(a, a, c) {
  // !!!  
  "use strict";
  return a + b + c; //    
}

, ECMAScript 5 8 . 8 ES5 , 0 (0644 === 420 "\045" === "%"). ECMAScript 2015 "0o" 8 .

js
var a = 0o10; // ES6: 8

0 , ! 8 .

js
"use strict";
var sum =
  015 + // !!!  
  197 +
  142;

, ECMAScript 6 primitive . (no-op), TypeError .

js
(function () {
  "use strict";

  false.true = ""; // TypeError
  (14).sailing = "home"; // TypeError
  "with".you = "far away"; // TypeError
})();

. X . JavaScript . JavaScript . .

, with . with ( ) . . with , with .

js
"use strict";
var x = 17;
with (obj) {
  // !!!  
  //  ,  var x   ,
  // obj.x   ?
  //        ,
  //     .
  x;
}

, with .

, eval . eval("var x;") x . , eval (eval ). eval evaluated , .

js
var x = 17;
var evalX = eval("'use strict'; var x = 42; x");
console.assert(x === 17);
console.assert(evalX === 42);

, eval eval(...) , evaluated . , .

js
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 .

js
"use strict";

var x;
delete x; // !!!  

eval("var y; delete y;"); // !!! syntax error

eval arguments

arguments eval . : eval, arguments. ECMAScript eval arguments .

, eval arguments . .

js
"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] .

js
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 , .

js
"use strict";
var f = function () {
  return arguments.callee;
};
f(); // TypeError

JavaScript ""

"" 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 :

js
"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 .

js
function restricted() {
  "use strict";
  restricted.caller; // throws a TypeError
  restricted.arguments; // throws a TypeError
}
function privilegedInvoker() {
  return restricted();
}
privilegedInvoker();

, . ECMAScript arguments.caller . ; . . , arguments.caller :

js
"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

ECMAScript , ECMAScript5 . , .

, . implements, interface, let, package, private, protected, public, static, yield. , , .

js
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 "". :

js
"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


Web Proxy Viewer  |  New URL  |  Original Page