From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../errors/unnamed_function_statement/index.html | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 files/ko/web/javascript/reference/errors/unnamed_function_statement/index.html (limited to 'files/ko/web/javascript/reference/errors/unnamed_function_statement') diff --git a/files/ko/web/javascript/reference/errors/unnamed_function_statement/index.html b/files/ko/web/javascript/reference/errors/unnamed_function_statement/index.html new file mode 100644 index 0000000000..b7afe67563 --- /dev/null +++ b/files/ko/web/javascript/reference/errors/unnamed_function_statement/index.html @@ -0,0 +1,118 @@ +--- +title: 'SyntaxError: function statement requires a name' +slug: Web/JavaScript/Reference/Errors/Unnamed_function_statement +tags: + - IIEF + - 객체 메소드 + - 구문 에러 + - 자바스크립트 + - 콜백 함수 + - 함수 이름 +translation_of: Web/JavaScript/Reference/Errors/Unnamed_function_statement +--- +
{{jsSidebar("Errors")}}
+ +

메세지

+ +
Syntax Error: Expected identifier (Edge)
+SyntaxError: function statement requires a name [Firefox]
+SyntaxError: Unexpected token ( [Chrome]
+
+ +

에러 타입

+ +

{{jsxref("SyntaxError")}}

+ +

무엇이 잘못되었을까?

+ +

함수 구문(Function statement)은 이름이 필수입니다. 함수가 정의된 방법에 따라 함수의 이름을 짓거나 함수 표현식(Function expression) {{Glossary("IIFE")}}으로 작성하거나, 함수가 맥락에 맞게 제대로 작성되었는지 확인해야 합니다.

+ +

예제

+ +

구문 vs 표현식

+ +

함수 구문(또는 함수 선언)은 이름이 필요하므로 아래 예제는 동작하지 않습니다:

+ +
function () {
+  return 'Hello world';
+}
+// SyntaxError: function statement requires a name
+
+ +

대신 함수 표현식을 사용할 수 있습니다:

+ +
var greet = function() {
+  return 'Hello world';
+};
+ +

또는, 선언하자마자 바로 실행되는 IIFE (Immediately Invoked Function Expression)를 사용할 수 있습니다. 이 경우 몇 개의 괄호가 더 필요합니다:

+ +
(function () {
+
+})();
+ +

레이블을 붙인 함수

+ +

만약 함수 레이블을 사용하는 경우 function 키워드 뒤에 함수 이름이 필요하므로 아래 예제는 동작하지 않습니다:

+ +
function Greeter() {
+  german: function () {
+    return "Moin";
+  }
+}
+// SyntaxError: function statement requires a name
+
+ +

아래 예제는 동작합니다:

+ +
function Greeter() {
+  german: function g() {
+    return "Moin";
+  }
+}
+ +

객체 메소드

+ +

만약 객체 메소드를 만드는 경우 먼저 객체를 만들어야 합니다. 객체 메소드의 경우 아래 예제와 같이 function 키워드 뒤에 이름이 없어도 정상적으로 동작합니다.

+ +
var greeter = {
+  german: function () {
+    return "Moin";
+  }
+};
+ +

콜백 구문

+ +

콜백을 사용하는 경우 구문을 확인해야 합니다. 괄호와 쉼표는 구문을 어렵게 만듭니다.

+ +
promise.then(
+  function() {
+    console.log("success");
+  });
+  function() {
+    console.log("error");
+}
+// SyntaxError: function statement requires a name
+
+ +

올바르게 변경하면:

+ +
promise.then(
+  function() {
+    console.log("success");
+  },
+  function() {
+    console.log("error");
+  }
+);
+
+ +

같이 보기

+ + -- cgit v1.2.3-54-g00ecf