SlideShare a Scribd company logo
1 of 14
Moving Ahead With
JavaScript v6
- Gaurav Behere
- http://www.gauravbehere.in
1. Quick Glance On JavaScript Evolution.
2. What is ES6 ?
3. Why Should We Bother?
4. Q/A.
JavaScript Evolution
JavaScript, was created in 10 days in May 1995 by Brendan Eich, working at Netscape and now of
Mozilla. JavaScript was not always known as JavaScript: the original name was Mocha, a name
chosen by Marc Andreessen, founder of Netscape.
In September of 1995 the name was changed to LiveScript, then in December of the same year,
upon receiving a trademark license from Sun, the name JavaScript was adopted. This was
somewhat of a marketing move at the time, with Java being very popular around then.
In 1996 - 1997 JavaScript was taken to ECMA to carve out a standard specification, which other
browser vendors could then implement based on the work done at Netscape. The work done over
this period of time eventually led to the official release of ECMA-262 Ed.1: ECMAScript is the
name of the official standard, with JavaScript being the most well known of the implementations.
ActionScript 3 is another well-known implementation of ECMAScript.
In July of 2008 the disparate parties on either side came together in Oslo. This led to the eventual
agreement in early 2009 to rename ECMAScript 3.1 to ECMAScript 5 and drive the language
forward using an agenda that is known as Harmony.
Current status: ECMA 6 draft is ready & published with pending approvals.
http://people.mozilla.org/~jorendorff/es6-draft.html
Compatibility:
https://kangax.github.io/compat-table/es6/
What is in ES6 ?
Highlights:
1. Better syntax, more verbose.
2. Modules, AMD, Async Module Definition.
3. Developer friendly APIs.
4. Better code structuring.
5. Lesser effort/ number of lines than JS v5.
A Bit of Deep Dive
De-structuring – A new and flexible way to assign values from arrays or objects into
variables.
Array de-structuring
var foo = ["one", "two"];
// without destructuring
var one = foo[0];
var two = foo[1];
// with destructuring
var [one, two] = foo;
Object de-structuring
var o = {p: 42, q: true};
var {p, q} = o;
console.log(p); // 42
console.log(q); // true
Block-level scope – ES6 now supports scoping variables to blocks (if, for, while, etc.) using
the let keyword.
Note: Variables declared by let have as their scope the block in which they are defined, as
well as in any contained sub-blocks. In this way, let works very much like var. The main
difference is that the scope of a var variable is the entire enclosing function.
Eg:
function varTest() {
var x = 31;
if (true) {
var x = 71; // same variable!
console.log(x); // 71
}
console.log(x); // 71
}
function letTest() {
let x = 31;
if (true) {
let x = 71; // different variable
console.log(x); // 71
}
console.log(x); // 31
}
Classes – ES6 classes provide a way to encapsulate and extend code.
Note: ES6 has only added a syntactical change to this as a function in JavaScript behaves as
a class.
Eg:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Constants – You can now define constants in ES6 code using the const keyword. (This is
already provided by Chrome since long)
Eg:
const my_fav = 7;
Default parameters – Ever wished that a function parameter could be assigned a default
value? You can do that now in ES6.
Eg:
function multiply(a, b = 1) {
return a*b;
}
multiply(5); // 5
Map – Dictionary type object that can be used to store key/value pairs.
Note: We were using array’s associative property to serve as a map, but this had limitations
like getting length & iteration was dependent on Object.keys
Eg:
var myMap = new Map();
myMap.set(“myKey”, “myValue”)
myMap.get(“myKey”); // “myValue”
Modules – Provides a modular way of organizing and loading code.
The usage & module definition is on the lines of commonJS.
Eg:
//File module.js
var someFunction(){
…..
}
var myModule = function(){
return someFunction()
}
export myModule;
//file client.js
Import myModule from ‘module.js’
myModule();
Promises – Used with asynchronous operations. Because async operations still have
problems.
Syntax:
new Promise(function(resolve, reject) { ... });
Rest parameters – Replaces the need for using arguments to access functions arguments.
Allows you to get to an array representing “the rest of the parameters”.
Eg:
function fun1(...theArgs) {
console.log(theArgs.length);
}
fun1(); // 0
fun1(5); // 1
fun1(5, 6, 7); // 3
Arrow functions – A short-hand version of writing an anonymous function.
Syntax:
([param] [, param]) => {
statements
}
param => expression
Eg:
var a2 = a.map(function(s){ return s.length });
Can be reduced to:
var a3 = a.map( s => s.length );
Generators – Specialized functions that can be paused & restarted & can be used as iterators,
declared using function* and the yield keyword.
Note: Calling a generator function does not execute its body immediately; an iterator object for the
function is returned instead. When the iterator's next() method is called, the generator function's
body is executed until the first yield expression, which specifies the value to be returned from the
iterator or, with yield*, delegates to another generator function. The next() method returns an object
with a value property containing the yielded value and a done property which indicates whether the
generator has yielded its last value
Run..Stop..Run
Syntax:
function* name([param[, param[, ... param]]]) {
statements
yield value;
}
Name.next().value;
Eg:
function* idMaker(){
var index = 0;
while(index < 3)
yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
….
Why Should We Bother ?
1. It is the future & we want to remain in the race.
2. Terminology & the language in which developers, websites,
blogs, references etc talk, will change.
3. Most importantly : The libraries which we are currently
depending on, are changing and adapting ES6 features.
Our ground is going to shake, let’s be prepared.
4. Dependency on requireJS for AMD, Kriskowal’s q for
Promises, Underscore for better Collection Processing etc
will be gone
Angular 2.0
1. Usage of classes.
2. Usage of modules.
3. Arrow functions.
4. Default parameters.
5. Rest parameters.
6. No controllers, scopes or
directives
Do watch : ES6 in Angular 2.0 by Erik Arvidsson at ng-europe 2014
https://www.youtube.com/watch?v=iij5RHKi66o
And Angular 2.0 Core by Igor Minar & Tobias Bosch at ng-europe 2014
https://www.youtube.com/watch?v=gNmWybAyBHI
Advertisement Time !!!

More Related Content

What's hot

iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS Multithreading
Richa Jain
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
 
Introduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDBIntroduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDB
Adrien Joly
 

What's hot (20)

CoffeeScript - TechTalk 21/10/2013
CoffeeScript - TechTalk 21/10/2013CoffeeScript - TechTalk 21/10/2013
CoffeeScript - TechTalk 21/10/2013
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS Multithreading
 
GCD and OperationQueue.
GCD and OperationQueue.GCD and OperationQueue.
GCD and OperationQueue.
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescript
 
Dispatch in Clojure
Dispatch in ClojureDispatch in Clojure
Dispatch in Clojure
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node Driver
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Alfresco the clojure way -- Slides from the Alfresco DevCon2011
Alfresco the clojure way -- Slides from the Alfresco DevCon2011Alfresco the clojure way -- Slides from the Alfresco DevCon2011
Alfresco the clojure way -- Slides from the Alfresco DevCon2011
 
Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners Clojure Fundamentals Course For Beginners
Clojure Fundamentals Course For Beginners
 
Java module configuration
Java module configurationJava module configuration
Java module configuration
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Introduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDBIntroduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDB
 
es6
es6es6
es6
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder Ruby
 
Automating with ansible (Part A)
Automating with ansible (Part A)Automating with ansible (Part A)
Automating with ansible (Part A)
 
WPF and Prism 4.1 Workshop at BASTA Austria
WPF and Prism 4.1 Workshop at BASTA AustriaWPF and Prism 4.1 Workshop at BASTA Austria
WPF and Prism 4.1 Workshop at BASTA Austria
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Java fork join
Java fork joinJava fork join
Java fork join
 

Viewers also liked (8)

Short intro to ES6 Features
Short intro to ES6 FeaturesShort intro to ES6 Features
Short intro to ES6 Features
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
Javascript Best Practice
Javascript Best Practice Javascript Best Practice
Javascript Best Practice
 
Javascript and Jquery Best practices
Javascript and Jquery Best practicesJavascript and Jquery Best practices
Javascript and Jquery Best practices
 
Intro to ES6 / ES2015
Intro to ES6 / ES2015Intro to ES6 / ES2015
Intro to ES6 / ES2015
 
10 Useful New Features of ECMA Script 6
10 Useful New Features of ECMA Script 610 Useful New Features of ECMA Script 6
10 Useful New Features of ECMA Script 6
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
 

Similar to Intro to ES6 and why should you bother !

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
旻琦 潘
 

Similar to Intro to ES6 and why should you bother ! (20)

ES6 - JavaCro 2016
ES6 - JavaCro 2016ES6 - JavaCro 2016
ES6 - JavaCro 2016
 
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
Javantura v3 - ES6 – Future Is Now – Nenad PečanacJavantura v3 - ES6 – Future Is Now – Nenad Pečanac
Javantura v3 - ES6 – Future Is Now – Nenad Pečanac
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Module design pattern i.e. express js
Module design pattern i.e. express jsModule design pattern i.e. express js
Module design pattern i.e. express js
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 
React native
React nativeReact native
React native
 
ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
A few good JavaScript development tools
A few good JavaScript development toolsA few good JavaScript development tools
A few good JavaScript development tools
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
Modern frontend in react.js
Modern frontend in react.jsModern frontend in react.js
Modern frontend in react.js
 

Recently uploaded

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Recently uploaded (20)

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 

Intro to ES6 and why should you bother !

  • 1. Moving Ahead With JavaScript v6 - Gaurav Behere - http://www.gauravbehere.in
  • 2. 1. Quick Glance On JavaScript Evolution. 2. What is ES6 ? 3. Why Should We Bother? 4. Q/A.
  • 3. JavaScript Evolution JavaScript, was created in 10 days in May 1995 by Brendan Eich, working at Netscape and now of Mozilla. JavaScript was not always known as JavaScript: the original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September of 1995 the name was changed to LiveScript, then in December of the same year, upon receiving a trademark license from Sun, the name JavaScript was adopted. This was somewhat of a marketing move at the time, with Java being very popular around then. In 1996 - 1997 JavaScript was taken to ECMA to carve out a standard specification, which other browser vendors could then implement based on the work done at Netscape. The work done over this period of time eventually led to the official release of ECMA-262 Ed.1: ECMAScript is the name of the official standard, with JavaScript being the most well known of the implementations. ActionScript 3 is another well-known implementation of ECMAScript. In July of 2008 the disparate parties on either side came together in Oslo. This led to the eventual agreement in early 2009 to rename ECMAScript 3.1 to ECMAScript 5 and drive the language forward using an agenda that is known as Harmony. Current status: ECMA 6 draft is ready & published with pending approvals. http://people.mozilla.org/~jorendorff/es6-draft.html Compatibility: https://kangax.github.io/compat-table/es6/
  • 4. What is in ES6 ? Highlights: 1. Better syntax, more verbose. 2. Modules, AMD, Async Module Definition. 3. Developer friendly APIs. 4. Better code structuring. 5. Lesser effort/ number of lines than JS v5.
  • 5. A Bit of Deep Dive De-structuring – A new and flexible way to assign values from arrays or objects into variables. Array de-structuring var foo = ["one", "two"]; // without destructuring var one = foo[0]; var two = foo[1]; // with destructuring var [one, two] = foo; Object de-structuring var o = {p: 42, q: true}; var {p, q} = o; console.log(p); // 42 console.log(q); // true
  • 6. Block-level scope – ES6 now supports scoping variables to blocks (if, for, while, etc.) using the let keyword. Note: Variables declared by let have as their scope the block in which they are defined, as well as in any contained sub-blocks. In this way, let works very much like var. The main difference is that the scope of a var variable is the entire enclosing function. Eg: function varTest() { var x = 31; if (true) { var x = 71; // same variable! console.log(x); // 71 } console.log(x); // 71 } function letTest() { let x = 31; if (true) { let x = 71; // different variable console.log(x); // 71 } console.log(x); // 31 }
  • 7. Classes – ES6 classes provide a way to encapsulate and extend code. Note: ES6 has only added a syntactical change to this as a function in JavaScript behaves as a class. Eg: class Polygon { constructor(height, width) { this.height = height; this.width = width; } } Constants – You can now define constants in ES6 code using the const keyword. (This is already provided by Chrome since long) Eg: const my_fav = 7; Default parameters – Ever wished that a function parameter could be assigned a default value? You can do that now in ES6. Eg: function multiply(a, b = 1) { return a*b; } multiply(5); // 5
  • 8. Map – Dictionary type object that can be used to store key/value pairs. Note: We were using array’s associative property to serve as a map, but this had limitations like getting length & iteration was dependent on Object.keys Eg: var myMap = new Map(); myMap.set(“myKey”, “myValue”) myMap.get(“myKey”); // “myValue” Modules – Provides a modular way of organizing and loading code. The usage & module definition is on the lines of commonJS. Eg: //File module.js var someFunction(){ ….. } var myModule = function(){ return someFunction() } export myModule; //file client.js Import myModule from ‘module.js’ myModule();
  • 9. Promises – Used with asynchronous operations. Because async operations still have problems. Syntax: new Promise(function(resolve, reject) { ... }); Rest parameters – Replaces the need for using arguments to access functions arguments. Allows you to get to an array representing “the rest of the parameters”. Eg: function fun1(...theArgs) { console.log(theArgs.length); } fun1(); // 0 fun1(5); // 1 fun1(5, 6, 7); // 3
  • 10. Arrow functions – A short-hand version of writing an anonymous function. Syntax: ([param] [, param]) => { statements } param => expression Eg: var a2 = a.map(function(s){ return s.length }); Can be reduced to: var a3 = a.map( s => s.length ); Generators – Specialized functions that can be paused & restarted & can be used as iterators, declared using function* and the yield keyword. Note: Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function. The next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value Run..Stop..Run
  • 11. Syntax: function* name([param[, param[, ... param]]]) { statements yield value; } Name.next().value; Eg: function* idMaker(){ var index = 0; while(index < 3) yield index++; } var gen = idMaker(); console.log(gen.next().value); // 0 console.log(gen.next().value); // 1 console.log(gen.next().value); // 2 console.log(gen.next().value); // undefined ….
  • 12. Why Should We Bother ? 1. It is the future & we want to remain in the race. 2. Terminology & the language in which developers, websites, blogs, references etc talk, will change. 3. Most importantly : The libraries which we are currently depending on, are changing and adapting ES6 features. Our ground is going to shake, let’s be prepared. 4. Dependency on requireJS for AMD, Kriskowal’s q for Promises, Underscore for better Collection Processing etc will be gone
  • 13. Angular 2.0 1. Usage of classes. 2. Usage of modules. 3. Arrow functions. 4. Default parameters. 5. Rest parameters. 6. No controllers, scopes or directives Do watch : ES6 in Angular 2.0 by Erik Arvidsson at ng-europe 2014 https://www.youtube.com/watch?v=iij5RHKi66o And Angular 2.0 Core by Igor Minar & Tobias Bosch at ng-europe 2014 https://www.youtube.com/watch?v=gNmWybAyBHI