Introduction
JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with
JavaScript.
Before moving further let us look at the previous articles of the series:
In the last article, we learned about ECMAScript 6 features and now we will learn some more cool features.
- Default values
We can add default values to function expressions. In ES5, the default value is undefined if the value is not passed via argument. Example ES5.
- function coordinates(x, y)
- {
- console.log(x);
- console.log(y);
- }
- coordinates(10);
In ES6 we will change it to,
- function coordinates(x=10, y=20)
- {
- console.log(x);
- console.log(y);
- }
- coordinates(10);
- Modules
This is the feature I like the most! We need to define each module in a file then export keyword to export. On the receiving side, we use import keyword to import. This modular approach is a really good solution for code reusability and abstraction.
Modules are designed around the export & import keywords.
- Classes
This is a favorite keyword of OOPS developers but JavaScript is prototypal based and function is a first-class citizen in JavaScript. Introducing classes in ES6 is debatable and many people have notion that it is not in favor of JavaScript prototypal nature.
Leaving aside, we can understand what lies in this feature.
Like OOPS programming languages, classes in ES6 are revolving around class & constructor. Example,
- class Automobile
- {
- constructor(type)
- {
- this.type = 'bike';
- this.engine = '150 CC';
- }
- getEngine()
- {
- return this.engine;
- }
- }
- var vehicle = new Automobile('Honda');
- console.log(vehicle.getEngine());
Inheritance
Use extends keyword for an inheritance to inherit from the base class. Example
- class LogView extends View
- {
- render()
- {
- var compiled = super.render();
- console.log(compiled);
- }
- }
- class LogView extends View {}
- Multi-line Strings
It’s a sugar syntax to cut long
- var s = 'hello '
- + 'world';
- console.log(s);
Summary
You can try these awesome features of ES6. Please share comments/ feedback.