ES6 for beginners with example

Srebalaji Thirumalai on September 29, 2017

In this post, I will cover some new features in ES6. It will be helpful if you are new to ES6 or learning front-end frameworks. Topics I’m gonna... [Read Full]
 

Thanks for this article! For get set I think it's more customary to use get name() and set name() instead of get getName() set setName(), but it's essentially the same.

class Person {
    constructor(name) {
      this._name = name;
    }
    get name() {
      return this._name;
    }
    set name(name) {
      this._name = name;
    }
}

let p = new Person("willie")
p.name = "jimmy"

console.log(p.name)
 
 

I'd like to point to the case of your get Name and set Name. Usually you keep those methods in lowercase to simulate an actual property so that who uses your class/object sees them as normal property retrieval/assignment.

@eerk example is perfect, since it is also good practice to name the actual property with the same name of the getter/setter methods prepended by an underscore

 

Nice post and good examples, thanks! Although I don't understand why so many examples use let over const. I think it's better to use const wherever you can. Is there a reason to consider the opposite?

 

Because let will never throw an error and is seen as the successor of var, while const is more restrictive.
You are right though, as long as you write ES6 always use const unless you see you explicitly need to change the variable reference, like for counters and booleans

 
 

Overall nice post and clear explanations, however, arrow functions are not just a new syntax for writing a function in Javascript. Regular functions still exist, and both of them behave differently in a matter of ways, most importantly in how they affect the scope and how this is bound in each of them. So more explanation should be done on that part. But still, nice post!

 

In two of your examples I think you reference the wrong variable.

let SumElements = (arr) => {
console.log(arr); // [10, 20, 40, 60, 90]
let sum = 0;
for (let element of arr) {
sum += arr;
}
console.log(sum); // 220.
}
SumElements([10, 20, 40, 60, 90]);

As well as in the following example of for of loops.

 
 

The case of the function examples is wrong, newOne and oldOne should start lowercase if you are trying to show good clean examples.

 
 

I really appreciate your post. Didn't know about this. I love to see this big improvement and it's really useful. Now I want to learn more

 
 

Thanks for the explanations, it cleared up some things in ES6 for me.

 

Thnx You have the gift of explain the things easily... More post how this pls

 
 
 

Thanks for this. I have also put together some useful examples for using rest & spread in es6.

smartdevnow.com/2017/06/02/es6-res...

 

This is awesome dude, thanks for taking the time to make it!

 

Nice post and thank you! The second expmple of 'Sets' is wrong in my editor, the keyword 'New' should be 'new'?

 
 
 
code of conduct - report abuse