📝 Edit page
➕ Add page
Methods
Class methods
Methods on classes can be set like this:
class Foo {
fizz(x) {
return x * 2
}
buzz = (x) => x * 2
}
Object methods
const const myObj = {
foo() {
// ...
},
bar(x) {
// ...
}
}
That is shorthand for:
const myObj = {
foo: function() {
// ...
},
bar: function(x) {
// ...
}
}
Example:
const myObj = {
a: 123,
fizz(x) {
return this.a + x
},
async buzz() {
await somePromise
}
// ...
}
myObj.fizz(1) // 124