Well for a long time I wanted to write a blog post about migrating Angular 1.x to 2, however, before doing so I thought it is really helpful to write about some prerequisites of that.
This is the first article in ES6 new features series.
Number one in the list would be ES6. Which is the topic of today’s post. ES6 (often referred to as “Harmony”) is the upcoming sixth major release of the ECMAScript language specification.
However, that does not put things in perspective. I prefer to say it is the newest JavaScript implementation, simple as that.
I just briefly list them here and go through one by one:
If you remember previously we had to explicitly specify a value if the coming parameter to a method didn’t have one.
var bar = function(foo) {
var temp = foo || 'default value'
}
Now it is like one of high level languages such as C#.
var bar = function(foo = 'default value') {
//code goes here
}
Template literals or interpolation in other languages is a way to output variables in the string. So in ES5 we had to break the string like this:
var fullname = firstname + ' ' + lastname
In ES6 though you can use the new syntax ${} which is again like C#:
var fullname = `Your name is ${firstname} ${lastname}.`
Previously we had to use + to shape multiple line strings something like this:
var fourAgreements =
'You have the right to be you.' + 'You can only be you when you do your best.'
But in ES6 they introduced the multi-line string literal ` which makes life easier:
var fourAgreements = `You have the right to be you.
You can only be you when you do your best.`
This can be a little confusing as there is some logic behind it. In ES5 when we wanted to extract some properties from an object we should define them as below:
var data = $('body').data(), // data has properties cat and mouse
house = data.cat,
mouse = data.mouse
In ES6, we can replace the ES5 code above with these statements:
var { cat, mouse } = $('body').data() // we'll get house and mouse variables
First of all let’s see what is an object literal. An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}
).
What you can do with object literals now in ES6 is mind blowing! It has started from something like a glorified JSON in ES5 to something like a class in ES6.
var sales = 'Toyota'
function carTypes(name) {
if (name === 'Honda') {
return name
} else {
return "Sorry, we don't sell " + name + '.'
}
}
var car = { myCar: 'Saturn', getCar: carTypes('Honda'), special: sales }
console.log(car.myCar) // Saturn
console.log(car.getCar) // Honda
console.log(car.special) // Toyota
Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options.
var car = { manyCars: { a: 'Saab', b: 'Jeep' }, 7: 'Mazda' }
console.log(car.manyCars.b) // Jeep
console.log(car[7]) // Mazda
In ES6 Object Literals are extended to support setting the prototype at construction, shorthand for foo: foo
assignments, defining methods, making super calls, and computing property names with expressions.
var obj = {
// __proto__
__proto__: theProtoObj,
// Shorthand for ‘handler: handler’
handler,
// Methods
toString() {
// Super calls
return 'd ' + super.toString()
},
// Computed (dynamic) property names
['prop_' + (() => 42)()]: 42,
}