SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
2013




JavaScript revolution
New syntax ( sugar of course! )
              &
      standard library
Who I am?


       Kamil Gałuszka
Django / Objective-C / Python World
JavaScript is only a hobby (for now)




       @galuszkak


       http://solution4future.com
Introduction & Inspiration
JavaScript, JS, ECMAScript, ES

● ECMAScript 3 ( 1999 )

● ECMAScript 4 ( incompatible with third version
  2005 )

● ECMAScript 5 ( 2009 )

● ECMAScript 5.1 ( 2011 )

● TC39 - Committee for Standardization
Funny Facts


● ES 5.1 standard == 243 pages

● JavaScript: The Definitive Guide by David
  Flanagan (6th edition)== 1078 pages

● Draft of standard ES 6.0 == 421 pages

● JavaScript: The Definitive Guide by David
  Flanagan (7th edition)== ? pages ;)
Warm Up

false == "0"               false === "0"
false == "false"           null === undefined
null == undefined          "" === 0
"" == 0
NaN === NaN
6.toString();
" nt" == 0;
obj = {x:10, y:20};
delete obj.y;
obj.y == null              obj.y === null
obj.y == undefined         obj.y === undefined
ES.Next vs ES.Harmony




        ES.Next === ECMAScript 6


ES 6 is fully compatible with ES5 (strict mode)


ES.Harmony in next versions of ECMAScript
Block Scope

Two new significant keywords:
let         lock scoped variable
const       read-only variable. (couldn't be hided by other variable etc.)

Typical problem in ES 5
    var result = [];
    for (var length = 10; length--;){
       var len = length;
       result[len] = function(){ return len; }
    }
    result[5]() // 0
    result[2]() // 0

WHY?
Block Scope
Solution ES 5

   var result = [];
   for (var length = 10; length--;){
      var len = length;
      result[len] = (function(dlugosc){
           return function(){
               return dlugosc;
           }
       })(len)
   }
   result[5]() // 5
   result[2]() // 2

use closure
Block Scope



Solution ES 6 (Cleaner, nicer, AWESOME!)

   var result = [];
   for (var length = 10; length--;){
      let len = length;
      result[len] = function(){ return len;}
   }
   result[5]() // 5
   result[2]() // 2
Shorthand Object Literals



ES 5 version:              ES 6 version:

var cat = "Molly";         var cat = "Molly";
var dog = "Rex";           var dog = "Rex";
var pets = {               var pets = {cat,dog}
    'cat': cat,
    'dog': dog,
}
Shorthand Object Literals


function Animal(name){
     this.name = name;
     this.age = 0;
}

Animal.prototype = {
  roar(text){
  //old version was function roar(text)
     return `Animal ${this.name}
  roars:${text}` // template syntax
  }
}
Destructing Assignments
      (extraction from objects, swap variables)




var {parse, stringify} = JSON;

var [, areaCode, local] = /^(d{3})-(d{3}-d
{4})$/.exec("048-032-7777");

[left, right] = [right, left];
Destructuring Nested Objects
var poets = [{
        "name": "T.S. Eliot",
        "works": [{
                 "title": "The Love Song",
                 "date": 1915
            }, {
                 "title": "Rhapsody",
                 "date": 1917
            }]
   }, {
        "name": "Ezra Pound",
        "works": [{
                 "title": "Ripostes",
                 "date": 1912
            }]
   }];
Destructuring Nested Objects




ES 5 VERSION ( really hard to remember )

var author = poets[0]['name'];
var title = poets[0]['works'][1]['title'];
var date = poets[0]['works'][1]['date'];
Destructuring Nested Objects


ES 6 version (AWESOME !)
  var [{'name': author, 'works': [, {title,
  date}]}] = poets;
  // Select first author and his second book
  and store it in variables author, title, date

  `"${title}", by ${author}, was published in
  ${date}.`
  // Template creating string from this
  variables.

Rhapsody, by T.S. Eliot, was published in 1917
Default parameters


// ES 5
function Person(name, age){
   this.name = name || "Anonim";
   this.age = age || 0;
}

// ES 6
function Person(name="Anonim", age=0)
{
   this.name = name;
  this.age = 0;
}
...rest parameters

js>function f(a, b, ...r) {
   print(Array.isArray(r));
   return r.concat(a, b);
}
js>f(1, 2)
true
[1, 2]
js>f(1, 2, 3)
true
[3, 1, 2]
js>f(1, 2, 3, 4, 5)
true
[3, 4, 5, 1, 2]
...spread

//ES 5 merging array
js> var a = [1, 2]
[3, 4, 5]
js> var b = [3, 4, 5]
[1, 2]
js> a = a.concat(b)
[1, 2, 3, 4, 5]


//ES 6 merging array (AWESOME!)
js> var a = [3, 4, 5]
[3, 4, 5]
js> var b = [1, 2, ...a]
[1, 2, 3, 4, 5]
Array Comprehensions




var arr =
[ x for (x of a) if (x.color === ‘blue’) ]

var arr = [ square(x) for (x of [1,2,3,4,5]) ]




                     Example by Addy Osmani http://addyosmani.com/blog/a-few-new-thin
                     to-javascript/
Modules




module Car {
  // import …
  // export …
}




       Example by Addy Osmani http://addyosmani.com/blog/a-few-new-
       things-coming-to-javascript/
Modules

module Car {
   // Internal
   var licensePlateNo = '556-343';
   // External
   export function drive(speed, direction) {
     console.log('details:', speed, direction);
   }
   export module engine{
     export function check() { }
   }
};
                     Example by Addy Osmani http://addyosmani.com/blog/a-few-new-
                     things-coming-to-javascript/
Imports
      (Yes! YES! No requirejs or AMD needed)




import       "crypto"       as       crypto;
import { encrypt, decrypt } from "crypto";
import { encrypt: enc } from "crypto";

import {drive, engine} from Car;




                        Example by Addy Osmani http://addyosmani.com/blog/a-few-new-
                        things-coming-to-javascript/
Classes
module widgets {
  // ...
  class DropDownButton extends Widget {
    constructor(attributes) {
      super(attributes);
      this.buildUI();
    }
    buildUI() {
      this.domNode.onclick = function(){
         // ...
      };
    }
  }
                      Example by Addy Osmani http://addyosmani.com/blog/a-few-new-
}                     things-coming-to-javascript/
Classes
var widgets = (function(global) {
  function DropDownButton(attributes) {
    Widget.call(this, attributes);
    this.buildUI();
  }
  DropDownButton.prototype = Object.create
(Widget.prototype, {
    constructor: { value: DropDownButton },
    buildUI:     {
      value: function(e) {
        this.domNode.onclick = function(e) {
          // ...
        }
                     Example by Addy Osmani http://addyosmani.com/blog/a-few-new-
}}})})(this);        things-coming-to-javascript/
Compatibility Matrix




http://kangax.github.com/es5-compat-table/es6/
Thanks to...



Douglas Crockford http://javascript.crockford.com/

Kit Cambridge http://kitcambridge.be/
(His speech: http://www.youtube.com/watch?v=Dt0f2XdvriQ)

TC39

Addy Osmani http://addyosmani.com/blog/a-few-new-things-
coming-to-javascript/

Contenu connexe

Tendances

Oral presentation v2
Oral presentation v2Oral presentation v2
Oral presentation v2
Yeqi He
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in Swift
Giordano Scalzo
 

Tendances (20)

Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
ES6 in Real Life
ES6 in Real LifeES6 in Real Life
ES6 in Real Life
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
JavaScript ES6
JavaScript ES6JavaScript ES6
JavaScript ES6
 
What's New in ES6 for Web Devs
What's New in ES6 for Web DevsWhat's New in ES6 for Web Devs
What's New in ES6 for Web Devs
 
Proxies are Awesome!
Proxies are Awesome!Proxies are Awesome!
Proxies are Awesome!
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
Async Frontiers
Async FrontiersAsync Frontiers
Async Frontiers
 
Alfresco the clojure way
Alfresco the clojure wayAlfresco the clojure way
Alfresco the clojure way
 
Oral presentation v2
Oral presentation v2Oral presentation v2
Oral presentation v2
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Introduction of ES2015
Introduction of ES2015Introduction of ES2015
Introduction of ES2015
 
LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기LetSwift RxSwift 시작하기
LetSwift RxSwift 시작하기
 
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in Swift
 
Planet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance EnhancementPlanet-HTML5-Game-Engine Javascript Performance Enhancement
Planet-HTML5-Game-Engine Javascript Performance Enhancement
 

En vedette

The Final Frontier
The Final FrontierThe Final Frontier
The Final Frontier
Domenic Denicola
 
Boom! Promises/A+ Was Born
Boom! Promises/A+ Was BornBoom! Promises/A+ Was Born
Boom! Promises/A+ Was Born
Domenic Denicola
 

En vedette (20)

ES6: The Awesome Parts
ES6: The Awesome PartsES6: The Awesome Parts
ES6: The Awesome Parts
 
The New JavaScript: ES6
The New JavaScript: ES6The New JavaScript: ES6
The New JavaScript: ES6
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
 
Promises, Promises
Promises, PromisesPromises, Promises
Promises, Promises
 
JavaScript 1.5 to 2.0 (TomTom)
JavaScript 1.5 to 2.0 (TomTom)JavaScript 1.5 to 2.0 (TomTom)
JavaScript 1.5 to 2.0 (TomTom)
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Real World Windows 8 Apps in JavaScript
Real World Windows 8 Apps in JavaScriptReal World Windows 8 Apps in JavaScript
Real World Windows 8 Apps in JavaScript
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
 
JavaScript im Jahr 2016
JavaScript im Jahr 2016JavaScript im Jahr 2016
JavaScript im Jahr 2016
 
Creating Truly RESTful APIs
Creating Truly RESTful APIsCreating Truly RESTful APIs
Creating Truly RESTful APIs
 
The Final Frontier
The Final FrontierThe Final Frontier
The Final Frontier
 
Client-Side Packages
Client-Side PackagesClient-Side Packages
Client-Side Packages
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
JavaScript on the Desktop
JavaScript on the DesktopJavaScript on the Desktop
JavaScript on the Desktop
 
Boom! Promises/A+ Was Born
Boom! Promises/A+ Was BornBoom! Promises/A+ Was Born
Boom! Promises/A+ Was Born
 
Webpack & EcmaScript 6 (Webelement #32)
Webpack & EcmaScript 6 (Webelement #32)Webpack & EcmaScript 6 (Webelement #32)
Webpack & EcmaScript 6 (Webelement #32)
 
Domains!
Domains!Domains!
Domains!
 
The jsdom
The jsdomThe jsdom
The jsdom
 
Webpack slides
Webpack slidesWebpack slides
Webpack slides
 
The State of JavaScript (2015)
The State of JavaScript (2015)The State of JavaScript (2015)
The State of JavaScript (2015)
 

Similaire à JavaScript - new features in ECMAScript 6

ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
qmmr
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 

Similaire à JavaScript - new features in ECMAScript 6 (20)

Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
MiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScriptMiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScript
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
ECMAScript2015
ECMAScript2015ECMAScript2015
ECMAScript2015
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
 
Short intro to ES6 Features
Short intro to ES6 FeaturesShort intro to ES6 Features
Short intro to ES6 Features
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8
 
ES6 in Production [JSConfUY2015]
ES6 in Production [JSConfUY2015]ES6 in Production [JSConfUY2015]
ES6 in Production [JSConfUY2015]
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
 
"let ECMAScript = 6"
"let ECMAScript = 6" "let ECMAScript = 6"
"let ECMAScript = 6"
 
recap-js.pdf
recap-js.pdfrecap-js.pdf
recap-js.pdf
 
Adding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer ToolboxAdding ES6 to Your Developer Toolbox
Adding ES6 to Your Developer Toolbox
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
Groovy
GroovyGroovy
Groovy
 
[React Native Tutorial] Lecture 3: More on ES6/ES2015
[React Native Tutorial] Lecture 3: More on ES6/ES2015[React Native Tutorial] Lecture 3: More on ES6/ES2015
[React Native Tutorial] Lecture 3: More on ES6/ES2015
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
ESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. Now
 

Dernier

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Dernier (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

JavaScript - new features in ECMAScript 6

  • 1. 2013 JavaScript revolution New syntax ( sugar of course! ) & standard library
  • 2. Who I am? Kamil Gałuszka Django / Objective-C / Python World JavaScript is only a hobby (for now) @galuszkak http://solution4future.com
  • 4. JavaScript, JS, ECMAScript, ES ● ECMAScript 3 ( 1999 ) ● ECMAScript 4 ( incompatible with third version 2005 ) ● ECMAScript 5 ( 2009 ) ● ECMAScript 5.1 ( 2011 ) ● TC39 - Committee for Standardization
  • 5. Funny Facts ● ES 5.1 standard == 243 pages ● JavaScript: The Definitive Guide by David Flanagan (6th edition)== 1078 pages ● Draft of standard ES 6.0 == 421 pages ● JavaScript: The Definitive Guide by David Flanagan (7th edition)== ? pages ;)
  • 6. Warm Up false == "0" false === "0" false == "false" null === undefined null == undefined "" === 0 "" == 0 NaN === NaN 6.toString(); " nt" == 0; obj = {x:10, y:20}; delete obj.y; obj.y == null obj.y === null obj.y == undefined obj.y === undefined
  • 7. ES.Next vs ES.Harmony ES.Next === ECMAScript 6 ES 6 is fully compatible with ES5 (strict mode) ES.Harmony in next versions of ECMAScript
  • 8. Block Scope Two new significant keywords: let lock scoped variable const read-only variable. (couldn't be hided by other variable etc.) Typical problem in ES 5 var result = []; for (var length = 10; length--;){ var len = length; result[len] = function(){ return len; } } result[5]() // 0 result[2]() // 0 WHY?
  • 9. Block Scope Solution ES 5 var result = []; for (var length = 10; length--;){ var len = length; result[len] = (function(dlugosc){ return function(){ return dlugosc; } })(len) } result[5]() // 5 result[2]() // 2 use closure
  • 10. Block Scope Solution ES 6 (Cleaner, nicer, AWESOME!) var result = []; for (var length = 10; length--;){ let len = length; result[len] = function(){ return len;} } result[5]() // 5 result[2]() // 2
  • 11. Shorthand Object Literals ES 5 version: ES 6 version: var cat = "Molly"; var cat = "Molly"; var dog = "Rex"; var dog = "Rex"; var pets = { var pets = {cat,dog} 'cat': cat, 'dog': dog, }
  • 12. Shorthand Object Literals function Animal(name){ this.name = name; this.age = 0; } Animal.prototype = { roar(text){ //old version was function roar(text) return `Animal ${this.name} roars:${text}` // template syntax } }
  • 13. Destructing Assignments (extraction from objects, swap variables) var {parse, stringify} = JSON; var [, areaCode, local] = /^(d{3})-(d{3}-d {4})$/.exec("048-032-7777"); [left, right] = [right, left];
  • 14. Destructuring Nested Objects var poets = [{ "name": "T.S. Eliot", "works": [{ "title": "The Love Song", "date": 1915 }, { "title": "Rhapsody", "date": 1917 }] }, { "name": "Ezra Pound", "works": [{ "title": "Ripostes", "date": 1912 }] }];
  • 15. Destructuring Nested Objects ES 5 VERSION ( really hard to remember ) var author = poets[0]['name']; var title = poets[0]['works'][1]['title']; var date = poets[0]['works'][1]['date'];
  • 16. Destructuring Nested Objects ES 6 version (AWESOME !) var [{'name': author, 'works': [, {title, date}]}] = poets; // Select first author and his second book and store it in variables author, title, date `"${title}", by ${author}, was published in ${date}.` // Template creating string from this variables. Rhapsody, by T.S. Eliot, was published in 1917
  • 17. Default parameters // ES 5 function Person(name, age){ this.name = name || "Anonim"; this.age = age || 0; } // ES 6 function Person(name="Anonim", age=0) { this.name = name; this.age = 0; }
  • 18. ...rest parameters js>function f(a, b, ...r) { print(Array.isArray(r)); return r.concat(a, b); } js>f(1, 2) true [1, 2] js>f(1, 2, 3) true [3, 1, 2] js>f(1, 2, 3, 4, 5) true [3, 4, 5, 1, 2]
  • 19. ...spread //ES 5 merging array js> var a = [1, 2] [3, 4, 5] js> var b = [3, 4, 5] [1, 2] js> a = a.concat(b) [1, 2, 3, 4, 5] //ES 6 merging array (AWESOME!) js> var a = [3, 4, 5] [3, 4, 5] js> var b = [1, 2, ...a] [1, 2, 3, 4, 5]
  • 20. Array Comprehensions var arr = [ x for (x of a) if (x.color === ‘blue’) ] var arr = [ square(x) for (x of [1,2,3,4,5]) ] Example by Addy Osmani http://addyosmani.com/blog/a-few-new-thin to-javascript/
  • 21. Modules module Car { // import … // export … } Example by Addy Osmani http://addyosmani.com/blog/a-few-new- things-coming-to-javascript/
  • 22. Modules module Car { // Internal var licensePlateNo = '556-343'; // External export function drive(speed, direction) { console.log('details:', speed, direction); } export module engine{ export function check() { } } }; Example by Addy Osmani http://addyosmani.com/blog/a-few-new- things-coming-to-javascript/
  • 23. Imports (Yes! YES! No requirejs or AMD needed) import "crypto" as crypto; import { encrypt, decrypt } from "crypto"; import { encrypt: enc } from "crypto"; import {drive, engine} from Car; Example by Addy Osmani http://addyosmani.com/blog/a-few-new- things-coming-to-javascript/
  • 24. Classes module widgets { // ... class DropDownButton extends Widget { constructor(attributes) { super(attributes); this.buildUI(); } buildUI() { this.domNode.onclick = function(){ // ... }; } } Example by Addy Osmani http://addyosmani.com/blog/a-few-new- } things-coming-to-javascript/
  • 25. Classes var widgets = (function(global) { function DropDownButton(attributes) { Widget.call(this, attributes); this.buildUI(); } DropDownButton.prototype = Object.create (Widget.prototype, { constructor: { value: DropDownButton }, buildUI: { value: function(e) { this.domNode.onclick = function(e) { // ... } Example by Addy Osmani http://addyosmani.com/blog/a-few-new- }}})})(this); things-coming-to-javascript/
  • 27. Thanks to... Douglas Crockford http://javascript.crockford.com/ Kit Cambridge http://kitcambridge.be/ (His speech: http://www.youtube.com/watch?v=Dt0f2XdvriQ) TC39 Addy Osmani http://addyosmani.com/blog/a-few-new-things- coming-to-javascript/