Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年9月.
自 ECMAScript 2015 開始,引入了一種於物件初始器(objects initializers)中定義方法的簡短語法。是一個將函式指派予方法名稱的簡便方式。
這個簡短的語法和在 ECMAScript 2015 引入 getter 以及 setter 類似。
請看以下程式碼:
你可以把它縮減為:
產生器方法(Generator method)也可以透過簡短語法定義之。用的時候:
Async 方法 也可以透過簡短語法定義。
Generator methods can also be async.
All method definitions are not constructors and will throw a TypeError if you try to instantiate them.
The shorthand syntax also supports computed property names.
| ECMAScript® 2027 Language Specification # sec-method-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月16日 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.