mozilla
Your Search Results

    Method definitions

    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Starting with ECMAScript 6, 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]) {},
      get property() {},
      set property(value) {},
      * generator() {}
    };
    

    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

    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
    ECMAScript 6 (ECMA-262)
    The definition of 'Method definitions' in that specification.
    Release Candidate Initial definition.

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Method definition shorthand 39 34 (34) Not supported 26 Not supported
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Method definition shorthand Not supported Not supported 34.0 (34) Not supported Not supported Not supported

    SpiderMonkey-specific notes

    • Prior to SpiderMonkey 38 (Firefox 38 / Thunderbird 38 / SeaMonkey 2.35),  "get" and "set" were invalid names for generator methods. This has been fixed in bug 1073809.

    See also

    Document Tags and Contributors

    Contributors to this page: ysangkok, jpmedley, fscholz
    Last updated by: fscholz,
    Hide Sidebar