SyntaxError: functions cannot be labelled
JavaScriptìì "functions cannot be labelled"ë¼ë ìì¸ë í¨ì ì ì¸ ìì ë ì´ë¸ì´ ìì ë ë°ìí©ëë¤.
ë©ìì§
SyntaxError: In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement. (V8-based) SyntaxError: In strict mode code, functions can only be declared at top level or inside a block. (V8-based) SyntaxError: Generators can only be declared at the top level or inside a block. (V8-based) SyntaxError: Async functions can only be declared at the top level or inside a block. (V8-based) SyntaxError: functions can only be labelled inside blocks (Firefox) SyntaxError: functions cannot be labelled (Firefox) SyntaxError: generator functions cannot be labelled (Firefox) SyntaxError: async function declarations can't appear in single-statement context (Firefox) SyntaxError: Unexpected keyword 'function'. Function declarations are only allowed inside block statements or at the top level of a program. (Safari) SyntaxError: Function declarations are only allowed inside blocks or switch statements in strict mode. (Safari) SyntaxError: Unexpected token '*'. Cannot use generator function declaration in single-statement context. (Safari) SyntaxError: Unexpected keyword 'function'. Cannot use async function declaration in single-statement context. (Safari)
ìë¬ íì
SyntaxError
무ìì´ ì못ëììê¹?
í¨ì ì ì¸ì ë ì´ë¸ì ë¶ì´ë ê²ì íì©ëì§ ììµëë¤. ë ì´ë¸ì ì ì¸ì´ ìëë¼ ë¬¸ìë§ ì ì©ëì´ì¼ í기 ë문ì ëë¤. ì´ ë ì´ë¸ë¡ ì¤ì ë¡ ë¶ê¸°í ë°©ë²ì´ ììµëë¤. ë¤ë§ ë ê±°ì JavaScript 구문 ê·ì¹ ë문ì, ì¤ë¥ê° ë°ìíë ì¡°ê±´ì´ íì ì´ìì¼ë¡ ë³µì¡í©ëë¤.
- ì격 모ëììë í¨ì ì ì¸ì ë ì´ë¸ì ì ë ë¶ì¼ ì ììµëë¤.
- ë¹ì격 모ëììë í¨ì ì ì¸ì ë ì´ë¸ì ë¶ì¼ ìë ìì§ë§, ê·¸ í¨ìê° if 문ì ì ì¼í ë¬¸ì¸ ê²½ì°ìë íì©ëì§ ììµëë¤(ì´ ìì²´ë ê¶ì¥ëì§ ìë 기ë¥ì ëë¤).
- ë¹ë기 í¨ì, ì ëë ì´í° í¨ì, ë¹ë기 ì ëë ì´í í¨ìë ë ì´ë¸ì ë¶ì¼ ì ììµëë¤.
ì¤ë¥ ë©ìì§ë "invalid place for a function declaration to appear"ì ë¹ì·íê² íìë ì ìëë°, íìê° ë ì´ë¸ì ë³´ë©´ ê·¸ ë¤ì ë¬¸ì´ ì¤ê¸°ë¥¼ 기ëíì§ë§ í¨ì ì ì¸ì ë¬¸ì´ ìë기 ë문ì ëë¤. ì´ë ì¤ë¥ê° ë ì´ë¸ ë¤ì í¨ì를 ë ì ìë¤ë ê´ì ì¸ì§, í¨ì ìì ë ì´ë¸ì´ ì¬ ì ìë¤ë ê´ì ì¸ì§ì ë°ë¼ ë¬ë¼ì§ëë¤.
ìì
>ìëì ë¤ë¥´ê² íì±ë ê°ì²´ 리í°ë´
ì¤ì ë¡ ë ì´ë¸ì´ ë¶ê¸° ëìì²ë¼ ëìí길 기ëíì ìë ìì§ë§, ì¼ë°ì ì¼ë¡ ë ì´ë¸ë¡ ìëí ê²ì ìëëë¤. ê°ì¥ íí ê²½ì°ë ì¤ì ë¡ ê°ì²´ 리í°ë´ìì ìì± í¤ë¡ ì¬ì©íë ¤ë ê²½ì°ì ëë¤.
const createObj = () => {
greet: function greet() { // SyntaxError: functions cannot be labelled
console.log("Hello");
}
};
ì¬ê¸°ì {...}ë ì¤ì ë¡ ê°ì²´ 리í°ë´ì´ ìëë¼ íì´í í¨ìì ë¸ë¡ 본문ì´ë¯ë¡ greet: ë ë ì´ë¸ì´ ë©ëë¤. ì´ë¥¼ í´ê²°íë ¤ë©´ ê°ì²´ 리í°ë´ì ê´í¸ë¡ ê°ì¸ì¼ í©ëë¤.
const createObj = () => ({
greet: function greet() {
console.log("Hello");
},
});
ê°ì²´ 리í°ë´ì ë©ìë 구문ì ì¬ì©íë©´ ì´ë¬í í¨ì ì í¼í ì ììµëë¤.
const createObj = () => ({
greet() {
console.log("Hello");
},
});