What’s New In ECMAScript 6 Part Two

ES6 really makes JS fun again. If you come across with our fist post, here we will discuss about objects and learn how awesome objects are in ES6. Let’s start.

Object Shorthand:

Imagine I have a dummy function. In real life it may fetch person’s information from a live server , in our case let’s hardcode it.

1

2

3

4

functiongetPerson(){

let name='Shibly';

let age=31;

}

Now typically in ES5 if you want to return the object, you need to do something like that,

1

2

3

4

5

6

7

8

9

10

functiongetPerson(){

let name='Shibly';

let age=31;

return{

name=name;

age=age;

}

}

Kind of annoying. But in ES6, we can get rid of somethings completely ! Like we can rewrite the same function like that :

1

2

3

4

5

6

7

functiongetPerson(){

let name='Shibly';

let age=31;

return{name,age}

}

In the situation when your property name is same as the value variable, in that case you can remove it entirely ! and you can clean up the code drastically.

You can try it like

1

alert(getPerson().name);

and it will return “Shibly”

Object De-structuring:

Imagine that:

1

2

3

4

5

let person={

name="Shibly";

age='31';

}

again let’s write

1

let{name,age}=person;

Now if I alert(name) , it will alert “Shibly”. Same for the age. So the values are assigned as we expected. This is what we call object de structuring. It is very similar to something like extract in php. It’s actually one of my favorite feature is ES6. It can save a lot of boilerplate code. Now let say you are making an ajax request and you get a data object in your response and it contains all of info you need. Like

1

2

3

4

5

6

7

8

let data={

name:"Shibly";

age:31;

result:['foo','bar'],

count:30

}

Now once you fetch that, typically you’d like to do something like :

1

2

varresults=data.result;

varcount=data.count;

For ES5 you are writing that sort of stuff all the time. But for ES6 we can say ,

1

let{result,count}=data;

If you console.log() both of those, you will get the object and the count value. How kool is that. However you can also use object de structuring as a function argument.

Shibly is a *nix fan, open source enthusiast and passionate software engineer. He Loves music, hacking and playing PC video games. Currently he holds a 980ti SLI rig ! He also likes to play with anything cool. He is also a partner & technical director at QuantikLab

Related Articles

GET A WEB DEVELOPMENT QUOTE

Do you have a question about web design and development? We are prepare to answer all of your questions. We always assist our clients to run business smoothly in a complete technology agnostic way. We are devoted to provide the best in class services by maintaining the timeline. We guarantee perfection. Send us your requirements, we'll extract it, do necessary RnD and will provide you something that will definitely amaze you.

WHAT OUR CLIENT THINKS

QuantikLab is absolutely the best, their vary reliable and always on time.

William alkins

We have worked with many different offshore web development companies in the past. But would just like to say that the people at QuantikLab are the best! Extremely professional yet very personable! Very clean and scalable work and always right on time! Since we started working with them, never had to use another development company for any of our web design needs.

Jeff Anderson

Follow Us

LinkedIn

Skype

GitHub

About Company

QuantikLab Is an Outsourcing Web Design Company and We provide the BEST possible web business solution to our client that current technology allows within their budget. Repeating business and long lasting relationship is very important to us and we strive to maintain that.