Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1030056 of Method definitions

  • Revision slug: Web/JavaScript/Reference/Functions/Method_definitions
  • Revision title: Method definitions
  • Revision id: 1030056
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment add ES2016 spec

Revision Content

{{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 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; // TypeError: obj.g is not a constructor (changed in ES2016)

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('ES7', '#sec-method-definitions', 'Method definitions')}} {{Spec2('ES7')}} Changed that generator methods should also not have a [[Construct]] trap and will throw when used with new.
{{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}}
Generator methods are not constructable (ES2016) {{CompatUnknown}} {{CompatGeckoDesktop("43")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Method definition shorthand {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile("34")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Generator methods are not constructable (ES2016) {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile("43")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

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)}}.

See also

Revision Source

<div>{{JsSidebar("Functions")}}</div>

<p>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.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
var obj = {
  <var>property</var>( <var>parameters…</var> ) {},
  *<var>generator</var>( <var>parameters…</var> ) {},
// also with computed keys:
  [property]( <var>parameters…</var> ) {},
  *[generator]( <var>parameters…</var> ) {},
// compare ES5 getter/setter syntax:
  get <var>property</var>() {},
  set <var>property</var>(<var>value</var>) {}
};
</pre>

<h2 id="Description">Description</h2>

<p>The shorthand syntax is similar to the <a href="/en-US/docs/Web/JavaScript/Reference/Functions/get">getter</a> and <a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">setter</a> syntax introduced in ECMAScript 5.</p>

<p>Given the following code:</p>

<pre class="brush: js">
var obj = {
  foo: function() {},
  bar: function() {}
};</pre>

<p>You are now able to shorten this to:</p>

<pre class="brush: js">
var obj = {
  foo() {},
&nbsp; bar() {}
};</pre>

<h3 id="Shorthand_generator_methods">Shorthand generator methods</h3>

<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">Generator methods</a> 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, <code>* g(){}</code> will work but&nbsp;<code>g *(){}</code> will not.</p>

<pre class="brush: js;highlight[12]">
// 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</pre>

<h3 id="Method_definitions_are_not_constructable">Method definitions are not constructable</h3>

<p>All method definitions are not constructors and will throw a {{jsxref("TypeError")}} if you try to instantiate them.</p>

<pre class="brush: js example-bad">
var obj = { 
  method() {},
};
new obj.method; // TypeError: obj.method is not a constructor

var obj = { 
  * g() {} 
};
new obj.g; // TypeError: obj.g is not a constructor (changed in ES2016)
</pre>

<h2 id="Examples">Examples</h2>

<h3 id="Simple_test_case">Simple test case</h3>

<pre class="brush: js;highlight[3]">
var obj = {
  a : "foo",
  b(){ return this.a; }
};
console.log(obj.b()); // "foo"
</pre>

<h3 id="Computed_property_names">Computed property names</h3>

<p>The shorthand syntax also supports computed property names.</p>

<pre class="brush: js;highlight[4]">
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</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-method-definitions', 'Method definitions')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES7', '#sec-method-definitions', 'Method definitions')}}</td>
   <td>{{Spec2('ES7')}}</td>
   <td>Changed that generator methods should also not have a [[Construct]] trap and will throw when used with <code>new</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-method-definitions', 'Method definitions')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Method definition shorthand</td>
   <td>{{CompatChrome("39")}}</td>
   <td>{{CompatGeckoDesktop("34")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera("26")}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Generator methods are not constructable (ES2016)</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("43")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Method definition shorthand</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("34")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Generator methods are not constructable (ES2016)</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("43")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="SpiderMonkey-specific_notes">SpiderMonkey-specific notes</h2>

<ul>
 <li>Prior to SpiderMonkey 38 {{geckoRelease(38)}},&nbsp; "<code>get</code>" and "<code>set</code>" were invalid names for generator methods. This has been fixed in {{bug(1073809)}}.</li>
 <li>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)}}).
  <pre class="brush: js example-bad">
var o = {x() 12}; // SyntaxError</pre>
 </li>
 <li>The restriction that only generator methods are constructors has been implemented in SpiderMonkey 41 {{geckoRelease(41)}}. See also {{bug(1059908)}} and {{bug(1166950)}}.</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/get">get</a></code></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">set</a></code></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar">Lexical grammar</a></li>
</ul>
Revert to this revision