Javascript the New Parts

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

description

Talk i gave at WebTech Conference on November 10th 2010. 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.webtechcon.it Follow me on Twitter! https://twitter.com/federicogalassi

Transcript of Javascript the New Parts

Page 2: Javascript the New Parts

• Short history of javascript

• State of the onion• The new parts

Page 3: Javascript the New Parts

Javascriptpublic in

1996

Page 4: Javascript the New Parts

No time to fix

Page 5: Javascript the New Parts

Standard 1999Ecmascript3rd edition

“Worst name ever”

Page 6: Javascript the New Parts

TC39Committee

Page 7: Javascript the New Parts

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

They couldn’t agree

Page 9: Javascript the New Parts

ES 3.1smallfixes

ES 4heavystuff

split

Page 10: Javascript the New Parts

ES 3.1Ecmascript5th edition

the winner

Page 11: Javascript the New Parts

the loser

ES 4Harmony

Page 12: Javascript the New Parts

ES5just fixes

javascript

Page 13: Javascript the New Parts

• Short history of javascript

• State of the onion• The new parts

Page 14: Javascript the New Parts

Betterobject oriented programming

Page 15: Javascript the New Parts

Javascriptis prototypal

surname: “simpsons”

simpsons

name: “bart”

bartprototype

bart.surname>  “simpsons”

Page 16: Javascript the New Parts

Wannabe classicalfunction  Simpsons(name)  {

this.name  =  name}

Simpsons.prototype.surname  =  “simpsons”

bart  =  new  Simpsons(“bart”)

constructor

class

object

Page 17: Javascript the New Parts

Ugly

Page 18: Javascript the New Parts

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

Simpleand

Powerful

Page 20: Javascript the New Parts

Back to the fatherhomer  =  Object.create(

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

Page 21: Javascript the New Parts

Getters and settershomer  =  {

beers:  3,get  drunk()  {

return  this.beers  >  5}

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

Page 22: Javascript the New Parts

Uniform access

Page 23: Javascript the New Parts

bart.age>  10

Propertieswere values

Page 24: Javascript the New Parts

Object.getOwnPropertyDescriptor(bart,  “age”

)>  {

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

}

Propertiesnow configurable

Page 25: Javascript the New Parts

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

})

Propertiescan be defined

Page 26: Javascript the New Parts

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

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

Better security

Page 29: Javascript the New Parts

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

configurableCan i delete it ?

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

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

Page 31: Javascript the New Parts

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

enumerable+

writablesecurity

Page 33: Javascript the New Parts

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

The road to safe mashups

Page 35: Javascript the New Parts

Better extensibility

Page 36: Javascript the New Parts

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

No more pollution

Page 38: Javascript the New Parts

Hide behavior

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

})

Page 39: Javascript the New Parts

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

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

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

The worldis yours

Page 43: Javascript the New Parts

Betterperformance

//  Native  json

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

Page 44: Javascript the New Parts

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

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

More operations on

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

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

Date.now()>  1289395540416

Page 47: Javascript the New Parts

//  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

JOY

Page 49: Javascript the New Parts

• Short history of javascript

• State of the onion• The new parts

Page 50: Javascript the New Parts

State of theOnion

Page 51: Javascript the New Parts

Onion becauseyou can start crying

Page 53: Javascript the New Parts

no IE6I don’t

shoot the red cross

Page 58: Javascript the New Parts

My pick is

chrome

chrome 7

Page 59: Javascript the New Parts

Modern Browsers

OK

Page 60: Javascript the New Parts

Old Browsers

ARGH

Page 61: Javascript the New Parts

The real problem

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

away in five years.”

Page 62: Javascript the New Parts

even worse

Page 63: Javascript the New Parts

Go figure

Page 64: Javascript the New Parts

we need a Miracle