SlideShare a Scribd company logo
1 of 63
Say hello to
ECMAScript 5


    by @kangax
What is ECMAScript?
•   Originated as JavaScript (actually, Mocha)
•   Standardized by ECMA International
•   JavaScript, JScript, ActionScript
•   5th edition in Dec ‘09
ECMAScript         5
 New meta-level control

Additions to language API

 Refinements, bug fixes

      Strict mode
New meta-level control
New meta-level control

{
      foo:   "string",
      bar:   3.14,
    "baz":   true,
        5:   [1,2,3]
}
New meta-level control

{
      foo:   "string",
      bar:   3.14,
    "baz":   true,
        5:   [1,2,3]
}
New meta-level control

{
      foo:     "string",
      bar:     3.14,
    "baz":     true,
        5:     [1,2,3]
}



             [[Writable]]
             [[Enumerable]]
             [[Configurable]]
New meta-level control

  [[Writable]] [[Enumerable]] [[Configurable]]


var object = { key: "value" };

// when writable
object.property = "new value";
object.property; // "new value"

// not writable
object.property = "new value";
object.property; // "value"
New meta-level control

  [[Writable]] [[Enumerable]] [[Configurable]]


var object = { key: "value" };

var keys = [];
for (var prop in object) {
  keys.push(prop);
}

keys;
// enumerable — ["key"]
// non-enumerable — []
New meta-level control

  [[Writable]] [[Enumerable]] [[Configurable]]


var object = { key: "value" };

delete object.key;

// configurable
object.key; // undefined

// non-configurable
object.key; // "value"
New meta-level control
     Property descriptors to the rescue


Object.defineProperty
Object.defineProperties
Object.create
New meta-level control
     Property descriptors to the rescue


Object.defineProperty({}, "key", {

 value: "foo bar baz",

  writable: true,
  enumerable: true,
  configurable: true
});
New meta-level control
     Property descriptors to the rescue


Object.defineProperty({}, "key", {

  value: "foo bar baz",

  writable: true,
  enumerable: true,
  configurable: true
});
New meta-level control
      Property descriptors to the rescue


Object.defineProperty({}, "key", {

  value: "foo bar baz",

  writable: true,
  enumerable: true,
  configurable: true
});                              Property descriptor
New meta-level control
      Property descriptors to the rescue


Object.defineProperty({}, "key", {

  value: "foo bar baz",

  writable: true,
  enumerable: true,
  configurable: true
});
New meta-level control
     Property descriptors to the rescue


Object.defineProperty({}, "key", {

 value: "foo bar baz",

  writable: true,
  enumerable: true,
  configurable: true
});
New meta-level control
     Property descriptors to the rescue


Object.defineProperty({}, "key", {

 value: "foo bar baz",

  writable: true,
  enumerable: true,
  configurable: true
});


               { key: "foo bar baz" }
New meta-level control
      Property descriptors to the rescue


Object.defineProperties({}, {
  key1: {
    value: "foo bar baz",
    enumerable: true
  },
  key2: {
    value: "foo bar baz",
    configurable: true
  }
});
New meta-level control
      Property descriptors to the rescue


Object.defineProperties({}, {
  key1: {
    value: "foo bar baz",
    enumerable: true
  },                       Property descriptors
  key2: {
    value: "trololololo",
    configurable: true
  }
});
New meta-level control
     Property descriptors to the rescue


Object.defineProperties({}, {
  key1: {
    value: "foo bar baz",
    enumerable: true
  },
  key2: {
    value: "trololololo",
    configurable: true
  }
});
New meta-level control
     Property descriptors to the rescue


Object.defineProperties({}, {
  key1: {
    value: "foo bar baz"
  },
  key2: {
    value: "trololololo"
  }
});
                    {
                      key1: "foo bar baz",
                      key2: "trololololo"
                    }
New meta-level control
     Property descriptors to the rescue


Object.create(parentObject, {
  key1: {
    value: "foo bar baz",
    enumerable: true
  },
  key2: {
    value: "foo bar baz",
    configurable: true
  }
});
New meta-level control
      Property descriptors to the rescue


Object.create(parentObject, {
  key1: {
    value: "foo bar baz",
    enumerable: true
  },                       Property descriptors
  key2: {
    value: "foo bar baz",
    configurable: true
  }
});
New meta-level control
      Property descriptors to the rescue


Object.create(parentObject, {
  key1: {
    value: "foo bar baz"
  },
  ...
});
                          [[Prototype]]
             {
                 key1: "foo bar baz",
                 key2: "trololololo"
             }
New meta-level control
                Examples


Object.defineProperty(Object.prototype,
  "clone", {
    value: function(props){
      return Object.create(this, props);
    }
  }
);
New meta-level control
                Examples


Object.defineProperty(Object.prototype,
  "clone", {
    value: function(props){
      return Object.create(this, props);
    }
  }
);
New meta-level control
                Examples

var john = {
  name: "John",
  skill: "Javascript"
};

var mike = john.clone({
  name: { value: "Mike" }
});

mike.name; // "Mike"
mike.skill; // "Javascript"
New meta-level control
                Examples

var john = {
  name: "John",
  skill: "Javascript"
};

var mike = john.clone({
  name: { value: "Mike" }
});

mike.name; // "Mike"
mike.skill; // "Javascript" (inherited)
New meta-level control
                Examples

var john = {
  name: "John",
  skill: "Javascript"
};

var mike = john.clone({
  name: { value: "Mike" }
});

for (var prop in mike) {
  console.log(prop); // "name" and "skill"
}
Additions to language API
Additions to language API

1) String.prototype.trim

 " hello world     ".trim(); // "hello world"

 var person = {
   name: " Joe ",
   trim: String.prototype.trim,
   toString: function(){
     return this.name;
   }
 };

 person.trim(); "Joe"
Additions to language API

1) String.prototype.trim

 " hello world     ".trim(); // "hello world"

 var person = {
   name: " Joe ",
   trim: String.prototype.trim,
   toString: function(){
     return this.name;
   }
 };

 person.trim(); "Joe"
Additions to language API


2) Array.prototype extensions

  [1,2,3].map(function(value){
    return value * 2;
  });

  // [2,4,6]
Additions to language API


2) Array.prototype extensions

  [1,2,3].map(function(value){
    return value * 2;
  });

  // [2,4,6]
Additions to language API


2) Array.prototype extensions

  [1,2,3,4,5,6].filter(function(value){
    return value > 3;
  });

  // [4,5,6]
Additions to language API


2) Array.prototype extensions

  [1,2,3,4,5,6].filter(function(value){
    return value > 3;
  });

  // [4,5,6]
Additions to language API


2) Array.prototype extensions

  [1,2,3,4,5,3].indexOf(3); // 2
  [1,2,3,4,5,3].lastIndexOf(3); // 5
Additions to language API


2) Array.prototype extensions

  [1,2,3,4,5,3].indexOf(3); // 2
  [1,2,3,4,5,3].lastIndexOf(3); // 5
Additions to language API


2) Array.prototype extensions

  [1,2,3,4].reduce(function(prev, curr){
    return prev + curr;
  });

  // 10

  1+2 -> 3
  3+3 -> 6
  6+4 -> 10
Additions to language API


2) Array.prototype extensions

  [1,2,3,4].reduce(function(prev, curr){
    return prev + curr;
  });

  // 10

  1+2 → 3
  3+3 → 6
  6+4 → 10
Additions to language API


2) Array.prototype extensions

  function sum(){
    return this.reduce(function(prev, curr){
      return prev + curr;
    });
  }
  Object.defineProperty(
    Array.prototype, "sum", { value: sum });

  [1,2,3].sum(); // 6
  [1,2,3,4,5].sum() // 15
Additions to language API


2) Array.prototype extensions

  function sum(){
    return this.reduce(function(prev, curr){
      return prev + curr;
    });
  }
  Object.defineProperty(
    Array.prototype, "sum", { value: sum });

  [1,2,3].sum(); // 6
  [1,2,3,4,5].sum() // 15
Refinements, bug fixes
Refinements, bug fixes


1) undefined, NaN, Infinity are non-writable

      if (undefined == foo){
        foo = { ... };
      }
Refinements, bug fixes


1) undefined, NaN, Infinity are non-writable

      if (undefined = foo){
        foo = { ... };
      }

      undefined; // foo (!)

      if (undefined == bar) {
        // might never be created
        bar = { ... };
      }
Refinements, bug fixes


2) reserved words as property names

   var myApp = {
     export: [module1, module2]
   };

   var chevy = new Car();
   chevy.class = "classic";
Refinements, bug fixes


      2) reserved words as property names

         var myApp = {
           export: [module1, module2] // boom!
         };
ES3
         var chevy = new Car();
         chevy.class = "classic"; // boom!
Refinements, bug fixes


      2) reserved words as property names

         var myApp = {
           export: [module1, module2] // OK!
         };
ES5
         var chevy = new Car();
         chevy.class = "classic"; // OK!
Refinements, bug fixes


    3) Tamper-proof built-ins

function Array(){
  alert("Stole your data!");
}

var privateData = [...];

// Vulnerable browsers — e.g. FF2
Refinements, bug fixes


    3) Tamper-proof built-ins

function Array(){
  alert("Stole your data!");
}

var privateData = [...];

// Vulnerable browsers — e.g. FF2
Refinements, bug fixes


          3) Tamper-proof built-ins

      function Array(){
        alert("Stole your data!");
      }

      var privateData = [...];


ES3    Data stolen   ES5   Built-in function is called
Strict mode
Strict mode


“use strict”
Strict mode
styles = {
  bodyColor: “red”,
  bodyBgColor: “green”,
  bodyColor: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.bodyColor;
    }
  }
}, 10);
Strict mode
styles = {
  color: “red”,
  bgColor: “green”,
                      Undeclared assignment
  color: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.color;
    }
  }
}, 10);
Strict mode
styles = {
  color: “red”,         Repeating property
  bgColor: “green”,
                             names
  color: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.color;
    }
  }
}, 10);
Strict mode
styles = {
  color: “red”,
  bgColor: “green”,
                      eval as argument name
  color: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.color;
    }
  }
}, 10);
Strict mode
styles = {
  color: “red”,         repeating argument
  bgColor: “green”,
                               names
  color: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.color;
    }
  }
}, 10);
Strict mode
styles = {
  color: “red”,
  bgColor: “green”,
                          with statement
  color: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.color;
    }
  }
}, 10);
Strict mode
styles = {
  color: “red”,
  bgColor: “green”,
                         arguments.callee
  color: “yellow”
}
setTimeout(function(x, eval, x){
  with(document) {
    if (!body) {
      setTimeout(arguments.callee, 10);
    }
    else {
      body.style.color = styles.color;
    }
  }
}, 10);
Browser support




http://kangax.github.com/es5-compat-table/
References

ECMA-262 5th ed. specification
http://www.ecma-international.org/publications/standards/Ecma-262.htm


ECMA-262 3rd ed. unofficial HTML version
http://bclary.com/2004/11/07/


es-discuss mailing lists
http://mail.mozilla.org/listinfo/es-discuss/
http://mail.mozilla.org/listinfo/es5-discuss/


comp.lang.javascript (here be dragons)
http://groups.google.com/group/comp.lang.javascript/
Thank you!




  by @kangax

More Related Content

What's hot

Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Swift internals
Swift internalsSwift internals
Swift internalsJung Kim
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8XSolve
 
Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)BoneyGawande
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testingVisual Engineering
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)David de Boer
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun KOCH
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기Arawn Park
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the ASTJarrod Overson
 
RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017Wanbok Choi
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsRoss Tuck
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
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...Domenic Denicola
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Aaron Gustafson
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021Ayesh Karunaratne
 

What's hot (20)

Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Swift internals
Swift internalsSwift internals
Swift internals
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the AST
 
RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017RxSwift 활용하기 - Let'Swift 2017
RxSwift 활용하기 - Let'Swift 2017
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
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...
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Say It With Javascript
Say It With JavascriptSay It With Javascript
Say It With Javascript
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
 

Viewers also liked

The Enumerable Module or How I Fell in Love with Ruby
The Enumerable Module or How I Fell in Love with RubyThe Enumerable Module or How I Fell in Love with Ruby
The Enumerable Module or How I Fell in Love with Rubyharisamin
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! EdholeEdhole.com
 
Turing Machine
Turing MachineTuring Machine
Turing MachineRajendran
 
FINITE STATE MACHINE AND CHOMSKY HIERARCHY
FINITE STATE MACHINE AND CHOMSKY HIERARCHYFINITE STATE MACHINE AND CHOMSKY HIERARCHY
FINITE STATE MACHINE AND CHOMSKY HIERARCHYnishimanglani
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Network Layer,Computer Networks
Network Layer,Computer NetworksNetwork Layer,Computer Networks
Network Layer,Computer Networksguesta81d4b
 
Finite State Machine | Computer Science
Finite State Machine | Computer ScienceFinite State Machine | Computer Science
Finite State Machine | Computer ScienceTransweb Global Inc
 
BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS Kak Yong
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedSlideShare
 

Viewers also liked (13)

The Enumerable Module or How I Fell in Love with Ruby
The Enumerable Module or How I Fell in Love with RubyThe Enumerable Module or How I Fell in Love with Ruby
The Enumerable Module or How I Fell in Love with Ruby
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
FINITE STATE MACHINE AND CHOMSKY HIERARCHY
FINITE STATE MACHINE AND CHOMSKY HIERARCHYFINITE STATE MACHINE AND CHOMSKY HIERARCHY
FINITE STATE MACHINE AND CHOMSKY HIERARCHY
 
Turing machines
Turing machinesTuring machines
Turing machines
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Network Layer,Computer Networks
Network Layer,Computer NetworksNetwork Layer,Computer Networks
Network Layer,Computer Networks
 
Finite state machine
Finite state machineFinite state machine
Finite state machine
 
Finite State Machine | Computer Science
Finite State Machine | Computer ScienceFinite State Machine | Computer Science
Finite State Machine | Computer Science
 
Cldch8
Cldch8Cldch8
Cldch8
 
Turing machine by_deep
Turing machine by_deepTuring machine by_deep
Turing machine by_deep
 
BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 

Similar to Say Hello to ECMAScript 5

HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: PrototypesVernon Kesner
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to ProtractorJie-Wei Wu
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascriptMiao Siyu
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用Felinx Lee
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macrosMarina Sigaeva
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairMark
 
Scala in practice
Scala in practiceScala in practice
Scala in practicepatforna
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 ReviewSperasoft
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)Anders Jönsson
 

Similar to Say Hello to ECMAScript 5 (20)

HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Javascript
JavascriptJavascript
Javascript
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
JS Level Up: Prototypes
JS Level Up: PrototypesJS Level Up: Prototypes
JS Level Up: Prototypes
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to Protractor
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macros
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Specs2
Specs2Specs2
Specs2
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 Review
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)
 

More from Juriy Zaytsev

Intro to front-end testing
Intro to front-end testingIntro to front-end testing
Intro to front-end testingJuriy Zaytsev
 
Interactive web with Fabric.js @ meet.js
Interactive web with Fabric.js @ meet.jsInteractive web with Fabric.js @ meet.js
Interactive web with Fabric.js @ meet.jsJuriy Zaytsev
 
Fabric.js @ Falsy Values
Fabric.js @ Falsy ValuesFabric.js @ Falsy Values
Fabric.js @ Falsy ValuesJuriy Zaytsev
 
Fabric.js — Building a Canvas Library
Fabric.js — Building a Canvas LibraryFabric.js — Building a Canvas Library
Fabric.js — Building a Canvas LibraryJuriy Zaytsev
 

More from Juriy Zaytsev (6)

Intro to front-end testing
Intro to front-end testingIntro to front-end testing
Intro to front-end testing
 
Interactive web with Fabric.js @ meet.js
Interactive web with Fabric.js @ meet.jsInteractive web with Fabric.js @ meet.js
Interactive web with Fabric.js @ meet.js
 
printio
printioprintio
printio
 
Fabric.js @ Falsy Values
Fabric.js @ Falsy ValuesFabric.js @ Falsy Values
Fabric.js @ Falsy Values
 
Fabric.js — Building a Canvas Library
Fabric.js — Building a Canvas LibraryFabric.js — Building a Canvas Library
Fabric.js — Building a Canvas Library
 
Prototype UI Intro
Prototype UI IntroPrototype UI Intro
Prototype UI Intro
 

Say Hello to ECMAScript 5

  • 1. Say hello to ECMAScript 5 by @kangax
  • 2. What is ECMAScript? • Originated as JavaScript (actually, Mocha) • Standardized by ECMA International • JavaScript, JScript, ActionScript • 5th edition in Dec ‘09
  • 3. ECMAScript 5 New meta-level control Additions to language API Refinements, bug fixes Strict mode
  • 5. New meta-level control { foo: "string", bar: 3.14, "baz": true, 5: [1,2,3] }
  • 6. New meta-level control { foo: "string", bar: 3.14, "baz": true, 5: [1,2,3] }
  • 7. New meta-level control { foo: "string", bar: 3.14, "baz": true, 5: [1,2,3] } [[Writable]] [[Enumerable]] [[Configurable]]
  • 8. New meta-level control [[Writable]] [[Enumerable]] [[Configurable]] var object = { key: "value" }; // when writable object.property = "new value"; object.property; // "new value" // not writable object.property = "new value"; object.property; // "value"
  • 9. New meta-level control [[Writable]] [[Enumerable]] [[Configurable]] var object = { key: "value" }; var keys = []; for (var prop in object) { keys.push(prop); } keys; // enumerable — ["key"] // non-enumerable — []
  • 10. New meta-level control [[Writable]] [[Enumerable]] [[Configurable]] var object = { key: "value" }; delete object.key; // configurable object.key; // undefined // non-configurable object.key; // "value"
  • 11. New meta-level control Property descriptors to the rescue Object.defineProperty Object.defineProperties Object.create
  • 12. New meta-level control Property descriptors to the rescue Object.defineProperty({}, "key", { value: "foo bar baz", writable: true, enumerable: true, configurable: true });
  • 13. New meta-level control Property descriptors to the rescue Object.defineProperty({}, "key", { value: "foo bar baz", writable: true, enumerable: true, configurable: true });
  • 14. New meta-level control Property descriptors to the rescue Object.defineProperty({}, "key", { value: "foo bar baz", writable: true, enumerable: true, configurable: true }); Property descriptor
  • 15. New meta-level control Property descriptors to the rescue Object.defineProperty({}, "key", { value: "foo bar baz", writable: true, enumerable: true, configurable: true });
  • 16. New meta-level control Property descriptors to the rescue Object.defineProperty({}, "key", { value: "foo bar baz", writable: true, enumerable: true, configurable: true });
  • 17. New meta-level control Property descriptors to the rescue Object.defineProperty({}, "key", { value: "foo bar baz", writable: true, enumerable: true, configurable: true }); { key: "foo bar baz" }
  • 18. New meta-level control Property descriptors to the rescue Object.defineProperties({}, { key1: { value: "foo bar baz", enumerable: true }, key2: { value: "foo bar baz", configurable: true } });
  • 19. New meta-level control Property descriptors to the rescue Object.defineProperties({}, { key1: { value: "foo bar baz", enumerable: true }, Property descriptors key2: { value: "trololololo", configurable: true } });
  • 20. New meta-level control Property descriptors to the rescue Object.defineProperties({}, { key1: { value: "foo bar baz", enumerable: true }, key2: { value: "trololololo", configurable: true } });
  • 21. New meta-level control Property descriptors to the rescue Object.defineProperties({}, { key1: { value: "foo bar baz" }, key2: { value: "trololololo" } }); { key1: "foo bar baz", key2: "trololololo" }
  • 22. New meta-level control Property descriptors to the rescue Object.create(parentObject, { key1: { value: "foo bar baz", enumerable: true }, key2: { value: "foo bar baz", configurable: true } });
  • 23. New meta-level control Property descriptors to the rescue Object.create(parentObject, { key1: { value: "foo bar baz", enumerable: true }, Property descriptors key2: { value: "foo bar baz", configurable: true } });
  • 24. New meta-level control Property descriptors to the rescue Object.create(parentObject, { key1: { value: "foo bar baz" }, ... }); [[Prototype]] { key1: "foo bar baz", key2: "trololololo" }
  • 25. New meta-level control Examples Object.defineProperty(Object.prototype, "clone", { value: function(props){ return Object.create(this, props); } } );
  • 26. New meta-level control Examples Object.defineProperty(Object.prototype, "clone", { value: function(props){ return Object.create(this, props); } } );
  • 27. New meta-level control Examples var john = { name: "John", skill: "Javascript" }; var mike = john.clone({ name: { value: "Mike" } }); mike.name; // "Mike" mike.skill; // "Javascript"
  • 28. New meta-level control Examples var john = { name: "John", skill: "Javascript" }; var mike = john.clone({ name: { value: "Mike" } }); mike.name; // "Mike" mike.skill; // "Javascript" (inherited)
  • 29. New meta-level control Examples var john = { name: "John", skill: "Javascript" }; var mike = john.clone({ name: { value: "Mike" } }); for (var prop in mike) { console.log(prop); // "name" and "skill" }
  • 31. Additions to language API 1) String.prototype.trim " hello world ".trim(); // "hello world" var person = { name: " Joe ", trim: String.prototype.trim, toString: function(){ return this.name; } }; person.trim(); "Joe"
  • 32. Additions to language API 1) String.prototype.trim " hello world ".trim(); // "hello world" var person = { name: " Joe ", trim: String.prototype.trim, toString: function(){ return this.name; } }; person.trim(); "Joe"
  • 33. Additions to language API 2) Array.prototype extensions [1,2,3].map(function(value){ return value * 2; }); // [2,4,6]
  • 34. Additions to language API 2) Array.prototype extensions [1,2,3].map(function(value){ return value * 2; }); // [2,4,6]
  • 35. Additions to language API 2) Array.prototype extensions [1,2,3,4,5,6].filter(function(value){ return value > 3; }); // [4,5,6]
  • 36. Additions to language API 2) Array.prototype extensions [1,2,3,4,5,6].filter(function(value){ return value > 3; }); // [4,5,6]
  • 37. Additions to language API 2) Array.prototype extensions [1,2,3,4,5,3].indexOf(3); // 2 [1,2,3,4,5,3].lastIndexOf(3); // 5
  • 38. Additions to language API 2) Array.prototype extensions [1,2,3,4,5,3].indexOf(3); // 2 [1,2,3,4,5,3].lastIndexOf(3); // 5
  • 39. Additions to language API 2) Array.prototype extensions [1,2,3,4].reduce(function(prev, curr){ return prev + curr; }); // 10 1+2 -> 3 3+3 -> 6 6+4 -> 10
  • 40. Additions to language API 2) Array.prototype extensions [1,2,3,4].reduce(function(prev, curr){ return prev + curr; }); // 10 1+2 → 3 3+3 → 6 6+4 → 10
  • 41. Additions to language API 2) Array.prototype extensions function sum(){ return this.reduce(function(prev, curr){ return prev + curr; }); } Object.defineProperty( Array.prototype, "sum", { value: sum }); [1,2,3].sum(); // 6 [1,2,3,4,5].sum() // 15
  • 42. Additions to language API 2) Array.prototype extensions function sum(){ return this.reduce(function(prev, curr){ return prev + curr; }); } Object.defineProperty( Array.prototype, "sum", { value: sum }); [1,2,3].sum(); // 6 [1,2,3,4,5].sum() // 15
  • 44. Refinements, bug fixes 1) undefined, NaN, Infinity are non-writable if (undefined == foo){ foo = { ... }; }
  • 45. Refinements, bug fixes 1) undefined, NaN, Infinity are non-writable if (undefined = foo){ foo = { ... }; } undefined; // foo (!) if (undefined == bar) { // might never be created bar = { ... }; }
  • 46. Refinements, bug fixes 2) reserved words as property names var myApp = { export: [module1, module2] }; var chevy = new Car(); chevy.class = "classic";
  • 47. Refinements, bug fixes 2) reserved words as property names var myApp = { export: [module1, module2] // boom! }; ES3 var chevy = new Car(); chevy.class = "classic"; // boom!
  • 48. Refinements, bug fixes 2) reserved words as property names var myApp = { export: [module1, module2] // OK! }; ES5 var chevy = new Car(); chevy.class = "classic"; // OK!
  • 49. Refinements, bug fixes 3) Tamper-proof built-ins function Array(){ alert("Stole your data!"); } var privateData = [...]; // Vulnerable browsers — e.g. FF2
  • 50. Refinements, bug fixes 3) Tamper-proof built-ins function Array(){ alert("Stole your data!"); } var privateData = [...]; // Vulnerable browsers — e.g. FF2
  • 51. Refinements, bug fixes 3) Tamper-proof built-ins function Array(){ alert("Stole your data!"); } var privateData = [...]; ES3 Data stolen ES5 Built-in function is called
  • 54. Strict mode styles = { bodyColor: “red”, bodyBgColor: “green”, bodyColor: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.bodyColor; } } }, 10);
  • 55. Strict mode styles = { color: “red”, bgColor: “green”, Undeclared assignment color: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.color; } } }, 10);
  • 56. Strict mode styles = { color: “red”, Repeating property bgColor: “green”, names color: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.color; } } }, 10);
  • 57. Strict mode styles = { color: “red”, bgColor: “green”, eval as argument name color: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.color; } } }, 10);
  • 58. Strict mode styles = { color: “red”, repeating argument bgColor: “green”, names color: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.color; } } }, 10);
  • 59. Strict mode styles = { color: “red”, bgColor: “green”, with statement color: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.color; } } }, 10);
  • 60. Strict mode styles = { color: “red”, bgColor: “green”, arguments.callee color: “yellow” } setTimeout(function(x, eval, x){ with(document) { if (!body) { setTimeout(arguments.callee, 10); } else { body.style.color = styles.color; } } }, 10);
  • 62. References ECMA-262 5th ed. specification http://www.ecma-international.org/publications/standards/Ecma-262.htm ECMA-262 3rd ed. unofficial HTML version http://bclary.com/2004/11/07/ es-discuss mailing lists http://mail.mozilla.org/listinfo/es-discuss/ http://mail.mozilla.org/listinfo/es5-discuss/ comp.lang.javascript (here be dragons) http://groups.google.com/group/comp.lang.javascript/
  • 63. Thank you! by @kangax

Editor's Notes