javascript the new parts v2

69
Javascript the New Parts v2 [email protected] slideshare.net/fgalassi

Upload: federico-galassi

Post on 15-Jan-2015

2.050 views

Category:

Technology


1 download

DESCRIPTION

Updated version of talk "Javascript the New Parts". I gave this at JsDay on May 12th 2011. I updated with latest stats and improved es5 coverage, most notably strict mode. Abstract: At last, ecmascript 5th edition is landing in all modern browsers. What are the new parts of the language and how can they help us to write better code? Also http://federico.galassi.net/ http://www.jsday.it/ Follow me on Twitter! https://twitter.com/federicogalassi

TRANSCRIPT

Page 2: Javascript the New Parts v2

• Short history of javascript

• State of the onion• The new parts

Page 3: Javascript the New Parts v2

Javascriptpublic in

1996

Page 4: Javascript the New Parts v2

No time to fix

Page 5: Javascript the New Parts v2

Standard 1999Ecmascript3rd edition

“Worst name ever”

Page 6: Javascript the New Parts v2

TC39Committee

Page 7: Javascript the New Parts v2

Years later...“It turns out that standard bodies are

not good places to innovate. That’s what laboratories and startups are

for. Standards must be drafted by consensus.”

http://yuiblog.com/blog/2008/08/14/premature-standardization/

Page 8: Javascript the New Parts v2

They couldn’t agree

Page 9: Javascript the New Parts v2

ES 3.1smallfixes

ES 4heavystuff

split

Page 10: Javascript the New Parts v2

ES 3.1Ecmascript5th edition

the winner

Page 11: Javascript the New Parts v2

the loser

ES 4Harmony

Page 12: Javascript the New Parts v2

ES5just fixes

javascript

Page 13: Javascript the New Parts v2

• Short history of javascript

• State of the onion• The new parts

Page 14: Javascript the New Parts v2

Betterobject oriented programming

Page 15: Javascript the New Parts v2

Javascriptis prototypal

surname: “simpsons”

simpsons

name: “bart”

bartprototype

bart.surname>  “simpsons”

Page 16: Javascript the New Parts v2

Wannabe classicalfunction  Simpsons(name)  {

this.name  =  name}

Simpsons.prototype.surname  =  “simpsons”

bart  =  new  Simpsons(“bart”)

constructor

class

object

Page 17: Javascript the New Parts v2

Ugly

Page 18: Javascript the New Parts v2

Create objects simpsons  =  {  surname:  “simpsons”  }

bart  =  Object.create(simpsons)bart.name  =  “bart”bart.age  =  10

hugo  =  Object.create(bart)hugo.name  =  “hugo”hugo.nature  =  “evil”

object

object

object

Page 19: Javascript the New Parts v2

Simpleand

Powerful

Page 20: Javascript the New Parts v2

Back to the fatherhomer  =  Object.create(

Object.getPrototypeOf(bart))homer.name  =  “homer”homer.age  =  38

Page 21: Javascript the New Parts v2

Getters and settershomer  =  {

beers:  3,get  drunk()  {

return  this.beers  >  5}

}homer.drunk>  falsehomer.beers  =  7homer.drunk>  true

Page 22: Javascript the New Parts v2

Uniform access

Page 23: Javascript the New Parts v2

bart.age>  10

Propertieswere values

Page 24: Javascript the New Parts v2

Object.getOwnPropertyDescriptor(bart,  “age”

)>  {

value:  10,enumerable:  true,writable:  true,configurable:  true

}

Propertiesnow configurable

Page 25: Javascript the New Parts v2

Object.defineProperty(bart,  “age”,  {value:  10,enumerable:  true,writable:  true,configurable:  true

})

Propertiescan be defined

Page 26: Javascript the New Parts v2

Object.defineProperties(bart,  {name:  {

value:  “bart”,enumerable:  true,writable:  true,configurable:  true

},age:  {

value:  10,enumerable:  true,writable:  true,configurable:  true

}})

More than one

Page 27: Javascript the New Parts v2

bart  =  Object.create(simpsons,  {name:  {

value:  “bart”,enumerable:  true,writable:  true,configurable:  true

},age:  {

value:  10,enumerable:  true,writable:  true,configurable:  true

}})

At creation time

Page 28: Javascript the New Parts v2

Better security

Page 29: Javascript the New Parts v2

writableCan i assign to it ?

Object.defineProperty(bart,  “age”,  {value:  10,writable:  false

})bart.age  =  5>  5bart.age>  10

Page 30: Javascript the New Parts v2

configurableCan i delete it ?

Object.defineProperty(bart,  “age”,  {value:  10,configurable:  false

})delete  bart.age>  falsebart.age>  10

Page 31: Javascript the New Parts v2

configurableCan i configure it ?

Object.defineProperty(bart,  “age”,  {value:  10,configurable:  false

})Object.defineProperty(bart,  “age”,  {

configurable:  true})>  TypeError:  Cannot  redefine  property

Page 32: Javascript the New Parts v2

enumerable+

writablesecurity

Page 33: Javascript the New Parts v2

Even moresecurity

//  Can’t  add  propertiesObject.preventExtensions(obj)//  !isExtensible  +  all  configurable  =  falseObject.seal(obj)//  isSealed  +  all  writable  =  falseObject.freeze(obj)

Object.isExtensible(obj)Object.isSealed(obj)Object.isFrozen(obj)

Page 34: Javascript the New Parts v2

The road to safe mashups

Page 35: Javascript the New Parts v2

Better extensibility

Page 36: Javascript the New Parts v2

enumerableDoes for/in show it up ?Object.defineProperty(bart,  “phobia”,  {

value:  “coffins”,enumerable:  false

})

//  Like  for/in  and  collect  keysObject.keys(bart)>  [“name”,  “surname”,  “age”]

Page 37: Javascript the New Parts v2

No more pollution

Page 38: Javascript the New Parts v2

Hide behavior

Object.defineProperty(bart,  “play”,  {value:  function()  {  ..play..  },enumerable:  false

})

Page 39: Javascript the New Parts v2

natives finallyextensible !

Object.defineProperty(Array.prototype,  “last”,  {

value:  function()  {return  this[this.length  -­‐  1]

},enumerable:  false

})

[4,3,5,1].last()>  1

Page 40: Javascript the New Parts v2

more nativewith getter

Object.defineProperty(Array.prototype,  “last”,  {

get:  function()  {return  this[this.length  -­‐  1]

},enumerable:  false

})

[4,3,5,1].last>  1

Page 41: Javascript the New Parts v2

works with DOM

Object.defineProperty(HTMLElement.prototype,  “classes”,  {

get:  function()  {return  this.getAttribute(“class”)                      .split(/\s+/)

},enumerable:  false

})

$(“<div  class=‘one  two’  />”).get(0).classes>  [“one”,  “two”]

Page 42: Javascript the New Parts v2

The worldis yours

Page 43: Javascript the New Parts v2

Betterperformance

//  Native  json

JSON.parse(string)JSON.stringify(object)

Page 44: Javascript the New Parts v2

Betterfunctional

programming[1,  2,  3].map(double)>  [2,  4,  6][2,  4,  6].every(isEven)>  true[1,  2,  3].filter(isEven)>  [2]

//  forEach,  some,  reduce,  reduceRight//  indexOf,  lastIndexOf

Page 45: Javascript the New Parts v2

Function.bind()var  bart  =  {

name:  “bart”}var  hello  =  function(greet)  {

return  greet  +  “i  am  “  +  this.name}

//  bind  to  this  and  partial  application(hello.bind(bart,  “hey”))()>  “hey,  i  am  bart”

Page 46: Javascript the New Parts v2

More operations on natives

Array.isArray([1,2,3])>  true

“        hello  world          ”.trim()>  “hello  world”

Date.now()>  1289395540416

(new  Date).toISOString()>  2010-­‐02-­‐20T05:52:53.649Z

Page 47: Javascript the New Parts v2

//  reserved  keyword  as  propertiesbart.class  =  “cartoon”//  abstract,  boolean,  byte,  char,  const  ...

//  OK  trailing  comma[1,  2,  3,  ]  

//  OK  trailing  comma{

name:  “bart”,}

//  8  instead  of  0  !!!parseInt(“08”)

No moreannoyances

Page 48: Javascript the New Parts v2

Strict mode

Page 49: Javascript the New Parts v2

http://hacks.mozilla.org/2011/01/ecmascript-5-strict-mode-in-firefox-4/

Invoking //  Globally“use  strict”;...  strict  code  ...

//  in  function  scopefunction  test()  {    “use  strict”;    ...  strict  code  ...}

Page 50: Javascript the New Parts v2

http://hacks.mozilla.org/2011/01/ecmascript-5-strict-mode-in-firefox-4/

Mistakes throw errors“use  strict”;

typo  =  5  //  no  var,  ERROR

NaN  =  10  //  invalid  assign

delete  Object.prototype  //  invalid  delete

var  o  =  {  p:  1,  p:  2  }  //  double  property  !?

function  dumb(p,  p)      //  double  parameter  !???

Page 51: Javascript the New Parts v2

http://hacks.mozilla.org/2011/01/ecmascript-5-strict-mode-in-firefox-4/

Securing javascript“use  strict”;

function  miracle()  {  return  this  }miracle()

//  undefined  !!!!!

Page 52: Javascript the New Parts v2

JOY

Page 53: Javascript the New Parts v2

• Short history of javascript

• State of the onion• The new parts

Page 54: Javascript the New Parts v2

State of theOnion

Page 55: Javascript the New Parts v2

Onion becauseyou can start crying

Page 57: Javascript the New Parts v2

no IE6I don’t

shoot the red cross

Page 62: Javascript the New Parts v2

My pick is

chrome

firefox 4

Page 63: Javascript the New Parts v2

Modern Browsers

OK

Page 64: Javascript the New Parts v2

Old Browsers

ARGH

Page 65: Javascript the New Parts v2

The real problem

“IE6 is fading very slowly. Five years ago I predicted that IE6 would fade

away in five years.”

Page 66: Javascript the New Parts v2

even worse

Page 67: Javascript the New Parts v2

Go figure

Page 68: Javascript the New Parts v2

we need a Miracle