{{JsSidebar("Functions")}}
Starting with ECMAScript 2015 (ES6), a shorter syntax for method definitions on objects initializers is introduced. It is a shorthand for a function assigned to the method's name.
Syntax
var obj = { property( parameters… ) {}, *generator( parameters… ) {}, // also with computed keys: [property]( parameters… ) {}, *[generator]( parameters… ) {}, // compare ES5 getter/setter syntax: get property() {}, set property(value) {} };
Description
The shorthand syntax is similar to the getter and setter syntax introduced in ECMAScript 5.
Given the following code:
var obj = { foo: function() {}, bar: function() {} };
You are now able to shorten this to:
var obj = { foo() {}, bar() {} };
Shorthand generator methods
Generator methods can be defined using the shorthand syntax as well. Note that the asterisk (*) in the shorthand syntax must be before the generator property name. That is, * g(){}
will work but g *(){}
will not.
// Using a named property (pre-ES6) var obj2 = { g: function*() { var index = 0; while(true) yield index++; } }; // The same object using shorthand syntax var obj2 = { * g() { var index = 0; while(true) yield index++; } }; var it = obj2.g(); console.log(it.next().value); // 0 console.log(it.next().value); // 1
Method definitions are not constructable
All method definitions except generator methods are not constructors and will throw a {{jsxref("TypeError")}} if you try to instantiate them.
var obj = { method() {}, }; new obj.method; // TypeError: obj.method is not a constructor var obj = { * g() {} }; new obj.g; // Generator
Examples
Simple test case
var obj = { a : "foo", b(){ return this.a; } }; console.log(obj.b()); // "foo"
Computed property names
The shorthand syntax also supports computed property names.
var bar = { foo0 : function (){return 0;}, foo1(){return 1;}, ["foo" + 2](){return 2;}, }; console.log(bar.foo0()); // 0 console.log(bar.foo1()); // 1 console.log(bar.foo2()); // 2
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-method-definitions', 'Method definitions')}} | {{Spec2('ES6')}} | Initial definition. |
{{SpecName('ESDraft', '#sec-method-definitions', 'Method definitions')}} | {{Spec2('ESDraft')}} |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Method definition shorthand | {{CompatChrome("39")}} | {{CompatGeckoDesktop("34")}} | {{CompatNo}} | {{CompatOpera("26")}} | {{CompatNo}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Method definition shorthand | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoMobile("34")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
SpiderMonkey-specific notes
- Prior to SpiderMonkey 38 {{geckoRelease(38)}}, "
get
" and "set
" were invalid names for generator methods. This has been fixed in {{bug(1073809)}}. - Prior to SpiderMonkey 41 {{geckoRelease(41)}}, curly braces were not required in method definitions. They are required from now on to conform to the ES6 specification and will throw a {{jsxref("SyntaxError")}} in this and later versions ({{bug(1150855)}}).
var o = {x() 12}; // SyntaxError
- The restriction that only generator methods are constructors has been implemented in SpiderMonkey 41 {{geckoRelease(41)}}. See also {{bug(1059908)}} and {{bug(1166950)}}.