-
Notifications
You must be signed in to change notification settings - Fork 22.5k
/
index.html
229 lines (182 loc) · 5.46 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
---
title: Method definitions
slug: Web/JavaScript/Reference/Functions/Method_definitions
tags:
- ECMAScript 2015
- Functions
- JavaScript
- Language feature
- Object
- Syntax
browser-compat: javascript.functions.method_definitions
---
<div>{{JsSidebar("Functions")}}</div>
<p>Starting with ECMAScript 2015, 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>
<div>{{EmbedInteractiveExample("pages/js/functions-definitions.html")}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="brush: js">const obj = {
get <var>property</var>() {},
set <var>property</var>(<var>value</var>) {},
<var>property</var>( <var>parameters…</var> ) {},
*<var>generator</var>( <var>parameters…</var> ) {},
async <var>property</var>( <var>parameters…</var> ) {},
async* <var>generator</var>( <var>parameters…</var> ) {},
// with computed keys
get [<var>property]</var>() {},
set [<var>property]</var>(<var>value</var>) {},
[<var>property</var>]( <var>parameters…</var> ) {},
*[<var>generator</var>]( <var>parameters…</var> ) {},
async [<var>property</var>]( <var>parameters…</var> ) {},
async* [<var>generator]</var>( <var>parameters…</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 ES5.</p>
<p>Given the following code:</p>
<pre class="brush: js">const obj = {
foo: function() {
// ...
},
bar: function() {
// ...
}
}
</pre>
<p>You are now able to shorten this to:</p>
<pre class="brush: js">const obj = {
foo() {
// ...
},
bar() {
// ...
}
}
</pre>
<h3 id="Generator_methods">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.</p>
<p>When doing so:</p>
<ul>
<li>The asterisk (<code>*</code>) in the shorthand syntax must be <em>before</em> the
generator property name. (That is, <code>* g(){}</code> will work,
but <code>g *(){}</code> will not.)</li>
<li>
<p>Non-generator method definitions cannot contain the <code>yield</code> keyword.
This means that <a
href="/en-US/docs/Web/JavaScript/Reference/Statements/Legacy_generator_function">legacy
generator functions</a> won't work either, and will throw a
{{jsxref("SyntaxError")}}. Always use <code>yield</code> in conjunction with the
asterisk (<code>*</code>).</p>
</li>
</ul>
<pre class="brush: js highlight[12]">// Using a named property
const obj2 = {
g: function* () {
let index = 0
while (true) {
yield index++
}
}
};
// The same object using shorthand syntax
const obj2 = {
* g() {
let index = 0
while (true) {
yield index++
}
}
};
const it = obj2.g()
console.log(it.next().value) // 0
console.log(it.next().value) // 1</pre>
<h3 id="Async_methods">Async methods</h3>
<p>{{jsxref("Statements/async_function", "Async methods", "", 1)}} can also be defined
using the shorthand syntax.</p>
<pre class="brush: js highlight[12]">// Using a named property
const obj3 = {
f: async function () {
await some_promise
}
}
// The same object using shorthand syntax
const obj3 = {
async f() {
await some_promise
}
}
</pre>
<h3 id="Async_generator_methods">Async generator methods</h3>
<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">Generator
methods</a> can also be {{jsxref("Statements/async_function", "async", "", 1)}}.</p>
<pre class="brush: js">const obj4 = {
f: async function* () {
yield 1
yield 2
yield 3
}
};
// The same object using shorthand syntax
const obj4 = {
async* f() {
yield 1
yield 2
yield 3
}
}</pre>
<h3 id="Method_definitions_are_not_constructable">Method definitions are not constructable
</h3>
<p>Methods cannot be constructors! They will throw a {{jsxref("TypeError")}} if you try to
instantiate them.</p>
<pre class="brush: js example-bad">const objA = {
method() {}
}
new objA.method // TypeError: obj.method is not a constructor
const objB = {
* g() {}
}
new objB.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]">const 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]">const 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
// A global function
function foo() {
return 1
}
let name = 'foo'
console.log(window[name]()) // 1
</pre>
<h2 id="Specifications">Specifications</h2>
{{Specifications}}
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat}}</p>
<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>