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
๋ฌด์์ด ์๋ชป ๋์์๊น์?
"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);
};
})();