- When defining functions within objects in ES5, we have to use the keyword function as follows:
const bicycle = {
gear: 2,
setGear: function(newGear) {
this.gear = newGear;
}
};
With ES6, you can remove the function keyword and colon altogether when defining functions in objects. Here's an example of this syntax: Here we just refactored the function setGear inside the object bicycle and used the shorthand syntax.
const bicycle = {
gear: 2,
setGear(newGear) {
this.gear = newGear;
}
};
bicycle.setGear(3);
console.log(bicycle.gear); will display 3
Top comments (1)
Nice