Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 9월.
ECMAScript 2015 를 시작으로, 객체 초기자(initializer)에 메서드 정의를 위한 더 짧은 구문이 도입되었습니다. 이는 메서드 명에 할당된 함수를 위한 단축입니다.
단축 구문은 ECMAScript 5에 도입된 getter 및 setter 구문과 비슷합니다.
다음 코드가 주어지면:
이제 이를 아래로 줄일 수 있습니다:
주의 : 단축 구문은 익명(anonymous) 함수 (…foo: function() {}… 에서처럼) 대신 유명(named) 함수를 사용합니다. 유명 함수는 함수 본체에서 호출될 수 있습니다 (이는 참조할 식별자가 없기에 익명 함수에게는 불가능합니다). 자세한 사항은, function 참조.
생성기 메서드는 단축 구문을 사용해서도 정의될 수 있습니다. 단축 구문 내 별표(*)는 생성기 속성명 앞에 와야 함을 주의하세요. 즉, * g(){}는 작동하지만 g *(){}는 아닙니다.
모든 메서드 정의는 생성자가 아니고 인스턴스화하려고 하는 경우 TypeError 예외가 발생합니다.
단축 구문은 속성 계산명(computed property name)도 지원합니다.
| ECMAScript® 2027 Language Specification # sec-method-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2023년 8월 28일 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.