Your SlideShare is downloading. ×
EcmaScript 5.1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

EcmaScript 5.1

783

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
783
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. EcmaScript5.1
    MASTERING NAMESPACES!
    Илья Кантор
    http://javascript.ru
  • 2. История
  • 3. varuser=<username="John">
    <fieldtype="age"value="25"/>
    <fieldtype="email"value="john@gmail.com"/>
    </user>
    alert(user.field.(@type=="email").@value)
    1997
    ECMA-262 1st edition
    1999
    ECMA-2623rd edition
    2001
    Compact Profile
    2004
    E4X

    ECMA-262 4th ed.
    2009
    ECMA-2625th edition
    ???

    ECMA-262Harmony
  • 4. ECMA-2625th edition
    Что нового?
  • 5. ECMA-2625th edition
    Багфиксы
  • 6. obj={
    class:'Menu'
    }
    ES3: ошибка
    ES5: ok
  • 7. functiontest(str){
    varre=/ok/g
    alert(re.test(str))
    }
    test("ok")
    test("ok")
    // true
    // ES3: false, ES5: true
  • 8. alert(parseInt("010")==parseFloat("010"))
    ES3: false
    ES5: true
  • 9. varobj={
    a:1,
    b:2,
    }
    vararr[1,2,3,]
    ES3: error
    ES5: ok
  • 10. МетаСвойства
  • 11. writable = false
    obj={
    class:'Menu'
    }
    obj.class='Bird‘
    =>obj.class == ‘Menu’
  • 12. configurable = false
    obj={
    class:'Menu'
    }
    deleteobj.class
    =>obj.class == ‘Menu’
  • 13. enumerable = false
    Object.prototype.each=...
    for(propin{}){
    // без свойства ‘each’
    }
  • 14. Объявление
    Object.defineProperty({},"class",
    {
    value:"Menu",
    writable:false,
    configurable:false,
    enumerable:true
    })
    => { “class” : “Menu” }
    property descriptor
  • 15. Объявление
    Object.defineProperties({},{
    class:{
    value:"Menu",
    writable:false,
    configurable:false
    },
    height:{
    value:200,
    configurable:false
    }
    })
    => { “class” : “Menu”, “height”: 200 }
  • 16. Закрытие объекта
    varuser={
    name:"Вася",
    /* ... */
    }
    Object.preventExtensions(user)
    user.a=5// Нельзя добавлять свойства
    Object.seal(user)
    deleteuser.name// Нельзя удалять свойства
    Object.freeze(user)
    user.name='Петя'// Нельзя менять свойства
  • 17. Наследование
    animal={
    canWalk:true
    }
    rabbit=Object.create(animal,{
    canRun:{
    value:true
    }
    })
    alert(rabbit.canWalk)// true
    Object.getPrototypeOf(rabbit) == animal // true
  • 18. Наследование
    rabbit=Object.create(animal,{
    canRun:{
    value:true
    }
    })
    bird=Object.create(Object.getPrototypeOf(rabbit),{
    canFly:{
    value:true
    }
    })
  • 19. Геттеры и Сеттеры
    user=Object.defineProperty({},"fullName",{
    get:function(){
    returnthis.firstName+' '+this.lastName
    },
    set:function(value){
    vars=value.trim().split(/s+/,2)
    this.firstName=s[0]; this.lastName=s[1]
    }
    })
     
    user.fullName="Вася Пупкин"
    alert(user.lastName)// Пупкин
  • 20. Геттеры и Сеттеры
    varuser={
    getfullName(){
    returnthis.firstName+' '+this.lastName
    },
    setfullName(value){
    vars=value.trim().split(/s+/,2)
    this.firstName=s[0]; this.lastName=s[1]
    }
    }
     
    user.fullName="Вася Пупкин"
    alert(user.lastName)// Пупкин
  • 21. JSON
  • 22. JSON
    event={
    title:"Conference",
    date:“today"
    }
    str=JSON.stringify(event)
    • {"title":"Conference","date":"today"}
    event =JSON.parse(str)
    @see https://github.com/douglascrockford/JSON-js
  • 23. JSON – любые объекты
    functionRoom(number){
    this.toJSON=function(){
    returnnumber
    }
    }
    event={
    title:"Conference",
    date:newDate(),
    room:newRoom(22)
    }
    JSON.stringify(event)
    {"title":"Conference","date":"2011-02-15T09:12:06.836Z","room":22}
  • 24. JSON – любые объекты
    functionRoom(number){
    this.toJSON=function(){
    returnnumber
    }
    }
    event={
    title:"Conference",
    date:newDate(), Date.prototype.toJSON
    room:newRoom(22)
    }
    JSON.stringify(event)
    {"title":"Conference","date":"2011-02-15T09:12:06.836Z","room":22}
  • 25. JSON.stringify(str, whitelist)
    event={
    title:"Conference",
    date:newDate(),
    domElement:document.body
    }
    JSON.stringify(event)
    => TypeError: Converting circular structure to JSON
    JSON.stringify(event,["title","date"])
    => {"title":"Conference","date":"2011-02-15T09:44:13.419Z"}
  • 26. JSON.stringify(str, replacer)
    event={
    title:"Conference",
    date:newDate(),
    domElement:document.body
    }
    JSON.stringify(event,function(key,value){
    returnvalue.nodeName?undefined:value
    })
    => {"title":"Conference","date":"2011-02-15T09:44:13.419Z"}
  • 27. JSON.parse(str)
    str='{"title":"Conference",
    "date":"2011-02-15T09:44:13.419Z"}'
    event=JSON.parse(str)
    пробелы
  • 28. JSON.parse(str)
    str='{"title":"Conference",
    "date":"2011-02-15T09:44:13.419Z"}'
    event=JSON.parse(str)
    event.date.getDay()
    => TypeError: no method 'getDay'
  • 29. JSON.parse(str, reviver)
    str='{"title":"Conference",
    "date":"2011-02-15T09:44:13.419Z"}'
    event=JSON.parse(str,function(key,value){
    if(key=='date'){
    returnnewDate(value)
    }
    returnvalue
    })
    event.date.getDay()
    => 2
  • 30. bind
  • 31. bind(this)
    functionButton(elem){
    this.sayHi=function(){
    alert('Hi')
    }
    elem.onclick=function(){
    this.sayHi()
    }.bind(this)
    }
  • 32. bind(this, args)
    functionButton(elem){
    this.say=function(phrase){
    alert(phrase)
    }
    elem.onclick=function(event,phrase){
    this.say(phrase)
    }.bind(this,'Hi')
    }
    @see http://www.prototypejs.org/api/function/bind
  • 33. Strict mode
  • 34. use strict
    "use strict"
    ... code ...
  • 35. use strict
    functionF(){
    "use strict"
    this.method=function(){
    // strict mode inherited
    }
    }
  • 36. use strict
    alert(010)// SyntaxError (octal literals deprecated)
    a=5// ReferenceError (undeclared a)
    obj.notWritable=...// TypeError
    deleteobj.notConfigurable// TypeError
    eval("var a = 5")
    alert(a)// ReferenceError (undeclared a)
    arguments.callee// TypeError
    arguments.caller// TypeError
    (function(){
    alert(this)// undefined вместо window
    })()
    with(..)// SyntaxError, 'with' statement
  • 37. Функции,которые давно ждали
    Object.keys(obj)
    "String".trim()
    Array.isArray(arr)
    [...].indexOf/lastIndexOf
    [...].forEach
    [...].map
    [...].filter
    [...].reduce/reduceRight
    // ...
    @seehttp://kangax.github.com/es5-compat-table/
  • 38. The future is now ?
    PrototypeJS
    ES5-shim
  • 39. Harmony
  • 40. It’s all real
    __noSuchMethod__Proxy.create
    letblock_scoped="yay!"
    constREALLY="srsly"
    #(x) { x * x }
    ifx>zreturn"без скобок"
    moduleIter="@std:Iteration"
    return[i*iforiinrange(n)]
    functionprintf(format,...args)ek_scoped= "yay!" consEALLY= "srsly"

×