SyntaxError: "use strict" not allowed in function with non-simple parameters

Message

Firefox:
SyntaxError: "use strict" not allowed in function with default parameter
SyntaxError: "use strict" not allowed in function with rest parameter
SyntaxError: "use strict" not allowed in function with destructuring parameter

Chrome:
SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list

Error type

SyntaxError.

๋ฌด์—‡์ด ์ž˜๋ชป ๋˜์—ˆ์„๊นŒ์š”?

 "use strict" ์ง€์‹œ๋ฌธ์€ ๋‹ค์Œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ค‘ ํ•œ ๊ฐœ๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜์˜ ๋งจ ์œ„์— ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค:

"use strict" ์ง€์‹œ๋ฌธ์€ ECMAScript ๋™์ž‘์— ๋”ฐ๋ผ ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜์˜ ๋งจ ์œ„์— ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Examples

๊ธฐ๋Šฅ ๋ช…์„ธ์„œ

๋‹ค์Œ์€ ํ•จ์ˆ˜ sum ์—๋Š” ๊ธฐ๋ณธ ๋งค๊ฐœ ๋ณ€์ˆ˜ a=1 ๋ฐ b=2๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค:

function sum(a = 1, b = 2) {
  // SyntaxError: "use strict" not allowed in function with default parameter
  'use strict';
  return a + b;
}

ํ•จ์ˆ˜๊ฐ€ strict mode ๋ชจ๋“œ์— ์žˆ์–ด์•ผ ํ•˜๊ณ  ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋˜๋Š” ํฌํ•จ๋œ ํ•จ์ˆ˜๊ฐ€ strict mode์— ์žˆ์–ด๋„ ๊ดœ์ฐฎ์œผ๋ฉด ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ "use strict" ์ง€์‹œ๋ฌธ์„ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

'use strict';
function sum(a = 1, b = 2) {
  return a + b;
}

ํ•จ์ˆ˜ ํ‘œํ˜„์‹

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ ๋˜ ๋‹ค๋ฅธ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

var sum = function sum([a, b]) {
  // SyntaxError: "use strict" not allowed in function with destructuring parameter
  'use strict';
  return a + b;
};

์ด๊ฒƒ์€ ๋‹ค์Œ ํ‘œํ˜„์‹์œผ๋กœ ๋ณ€ํ™˜ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

var sum = (function() {
  'use strict';
  return function sum([a, b]) {
    return a + b;
  };
})();

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ this์— ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š”,  ๋‘˜๋Ÿฌ์‹ผ ํ•จ์ˆ˜๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

var callback = (...args) => {
  // SyntaxError: "use strict" not allowed in function with rest parameter
  'use strict';
  return this.run(args);
};

์ด๊ฒƒ์€ ๋‹ค์Œ ํ‘œํ˜„์‹๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

var callback = (() => {
  'use strict';
  return (...args) => {
    return this.run(args);
  };
})();

See also