Skip to content

Latest commit

 

History

History
61 lines (50 loc) · 1.76 KB

cl4ikwc540ax0h2nv6wygdz5r.md

File metadata and controls

61 lines (50 loc) · 1.76 KB

Learn Concise Declarative Functions with ES6 in JavaScript.

Hello everyone, hope you are all doing well. My name is Surya L, and the aim of this blog is to teach you all about Learn Concise Declarative Functions with ES6 in JavaScript.

Concise Declarative Functions with ES6

When defining functions within objects in ES5, we have to use the keyword function as follows:

Example1:

const person = {
  name: "Taylor",
  sayHello: function() {
    return `Hello! My name is ${this.name}.`;
  }
};

With ES6, you can remove the function keyword and colon altogether when defining functions in objects. Here's an example of this syntax:

const person = {
  name: "Taylor",
  sayHello() {
    return `Hello! My name is ${this.name}.`;
  }
};
console.log(person.sayHello());

We can call the function sayHello using person.sayHello() outside the object and the output for the above code is

**Hello! My name is Taylor. **

Example2:

const bicycle = {
  gear: 2,
  setGear(newGear) {
    this.gear = newGear;
  }
};
bicycle.setGear(3);
console.log(bicycle.gear);

The output of the above code is 3

  • Because initially the value of gear is 2,but after bicycle.setGear(3); is executed the value of the gear is set to 3 by setGear function.
  • So the value of gear is 3.

Thanks for reading the blog. Do let me know what you think about it.

You can connect me with Showwcase Twitter Blog GitHub Contact Me

Refernce: I learned this topics in FreeCodeCamp which I explained in minified version