9. ES6 Features
● Modules
● Class
● Block-level scoping
● Arrow Function
● Default Value, Rest, Spread Operator
● Destructuring
● Template Strings
● Object Literal
● Promise
● Symbol
● Generators as Iterators
● String new features
● Array new features
● Object new features
● Proxy
● ...
11. Modules
● Allow to load code & to provide a level of abstraction.
● Not support fully yet, have to use transpiler like Babel to transpile into
ES5.
12. Export
Two ways to export from a module.
● Multiple named exports
● Single default export
● Export with a more convenient alias
13.
14. Import
Flexible way to import from exported module:
● Import a single export
● Import multiple exports
● Import an export with a more convenient alias
● Dynamic imports
17. Class
A class definition can include the following:
● Constructors − Responsible for allocating memory for the objects of the
class.
● Functions − Functions represent actions an object can take. They are also
at times referred to as methods.
● Getter, setter: Run code for reading, writing of a property.
44. Generators as Iterators
Is special type of function that when invoked automatically generates a special
iterator, called a generator object.
Generator functions are indicated by function* and make use of the yield
operator to indicate the value to return for each successive call to .next() on
the generator.
46. Generators as Iterators
// call .next() to return an object
generatorObj.next();
{ value: "Generators", done: false }
// til the last of iterators
generatorObj.next();
{ value: undefined, done: true }
// jump to the last of iterators
generatorObj.return();
{ value: undefined, done: true }
47. yield*
Basically yield* is iterating over the values of an iterable for us and then
yielding those values individually
48. yield*
function* awesomeYield() {
yield 'Awesome Yield!!!';
yield* ['Im', 'Iron', 'Man'];
yield 'The end';
};
…………………
const yeildAwe = awesomeYield();
// Awesome Yield!!! Im Iron Man The end
49. with spread operator, destructuring
function* awesomeYield() {
yield 'Awesome Yield!!!';
yield* ['Im', 'Iron', 'Man'];
yield 'The end';
};
// Awesome Yield!!! Im Iron Man The end
const yeildAwe = awesomeYield();
// with destructuring
const [first, second, ...rest] = yeildAwe;
// with spread operator
const all = [...yeildAwe];
51. Promise
A way to implement asynchronous programming in Javascript.
const promise = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if (/* everything turned out fine */) {
resolve("Stuff worked!");
}
else {
reject(Error("It broke"));
}
});
56. Without Proxy
let target = {
name: "My name is Proxy",
count: 1
};
target.count = "I can change anything here.";
target.description = "Easy to add more!";
delete target.count; // I can delete also
;
57. Proxy
Proxies are wrappers that can intercept and alter low-level JavaScript engine
operations to allow developers to create built-in objects
let proxy = new Proxy(target, traps);
58. With Proxy
const target = {
name: "My Proxy",
count: 1
};
const set = (trapTarget, key, value, receiver) => {
if (isNaN(value)) {
throw new TypeError("Property must be a
number.");
}
// add the property
return Reflect.set(trapTarget, key, value,
receiver);
};
const get = (trapTarget, key, receiver) => {
/* do something */
};
const traps = {
get,
set
};
let proxy = new Proxy(target, traps);
May 1995: JavaScript was developed by Brendan Eich, a developer at Netscape under name “Mocha”.
September 1995: Released as LiveScript.
December 1995: Renamed Javascript.
November 1996: Netscape submitted Javascript to ECMA International for standardization.
June 1997: This resulted in a new language standard, known as ECMAScript.
ECMAScript (ES) is a scripting language specification standardized by ECMAScript International. It is used by applications to enable client-side scripting. The specification is influenced by programming languages like Self, Perl, Python, Java etc.JavaScript was developed by Brendan Eich, a developer at Netscape Communications Corporation, in 1995.JavaScript started life with the name Mocha, and was briefly named LiveScript before being officially renamed to JavaScript. It is a scripting language that is executed by the browser, i.e. on the client’s end. It is used in conjunction with HTML to develop responsive web pages.
ECMAScript 6, also known as ECMAScript 2015 was published in June 2015, is the latest version of the ECMAScript standard. ES6 is a significant update to the language, and the first update to the language since ES5 was standardized in 2009.
Web browser support for the full language is not yet complete, though major portions are supported. Major web browsers support some features of ES6. However, it is possible to use software known as a transpiler to convert ES6 code into ES5, which is better supported on most browsers.
A module is nothing more than a chunk of JavaScript code written in a file. The functions or variables in a module are not available for use, unless the module file exports them.
In simpler terms, the modules help you to write the code in your module and expose only those parts of the code that should be accessed by other parts of your code.
ES6 modules will have to be transpiled to ES5 code so that we can run and test the code. Transpilation is the process of converting code from one language into its counterpart equivalent. The ES6 Module Transpiler is a tool that takes your ES6 module and compiles it into ES5 compatible code in the CommonJS or AMD style.
A chunk of JavaScript code written in a file. The functions or variables in a module are not available for use, unless the module file exports them.
Object-Oriented Programming Concepts
To begin with, let us understand
Object − An object is a real-time representation of any entity. According to Grady Brooch, every object is said to have 3 features −
State − Described by the attributes of an object.
Behavior − Describes how the object will act.
Identity − A unique value that distinguishes an object from a set of similar such objects.
Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object.
Method − Methods facilitate communication between objects.
Let us translate these Object-Oriented concepts to the ones in the real world. For example: A car is an object that has data (make, model, number of doors, Vehicle Number, etc.) and functionality (accelerate, shift, open doors, turn on headlights, etc.)
Prior to ES6, creating a class was a fussy affair. Classes can be created using the class keyword in ES6.
ECMAScript 6 gives developers further access to JavaScript engine capabilities by adding built-in objects. To allow developers to create built-in objects, the language exposes the inner workings of objects through proxies, which are wrappers that can intercept and alter low-level JavaScript engine operations.