April 30, 2021 08:50 pm GMT
Original Link: https://dev.to/rthefounding/write-concise-declarative-functions-with-es6-1p5c
Write Concise Declarative Functions with ES6
- 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
Original Link: https://dev.to/rthefounding/write-concise-declarative-functions-with-es6-1p5c
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To