ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
EcmaScript5.1MASTERING NAMESPACES!§ª§Ý§î§ñ §¬§Ñ§ß§ä§à§âhttp://javascript.ru
§ª§ã§ä§à§â§Ú§ñ
varuser=<username="John"><fieldtype="age"value="25"/><fieldtype="email"value="john@gmail.com"/></user>alert(user.field.(@type=="email").@value)1997ECMA-262 1st edition1999ECMA-2623rd edition 2001Compact Profile 2004E4X   ¡ª ECMA-262 4th ed.2009ECMA-2625th edition ???   ¡ªECMA-262Harmony
ECMA-2625th edition §¹§ä§à §ß§à§Ó§à§Ô§à?
ECMA-2625th edition §¢§Ñ§Ô§æ§Ú§Ü§ã§í
obj={class:'Menu'}ES3: §à§ê§Ú§Ò§Ü§ÑES5: ok
functiontest(str){varre=/ok/galert(re.test(str))}test("ok")test("ok")// true// ES3: false,  ES5: true
alert(parseInt("010")==parseFloat("010"))ES3: falseES5: true
varobj={a:1,b:2,}vararr[1,2,3,]ES3: errorES5: ok
§®§Ö§ä§Ñ§³§Ó§à§Û§ã§ä§Ó§Ñ
writable = falseobj={class:'Menu'}obj.class='Bird¡®=>obj.class == ¡®Menu¡¯
configurable = falseobj={class:'Menu'}deleteobj.class=>obj.class == ¡®Menu¡¯
enumerable = falseObject.prototype.each=...for(propin{}){// §Ò§Ö§Ù §ã§Ó§à§Û§ã§ä§Ó§Ñ ¡®each¡¯}
§°§Ò§ì§ñ§Ó§Ý§Ö§ß§Ú§ÖObject.defineProperty({},"class",{value:"Menu",writable:false,configurable:false,enumerable:true})=> { ¡°class¡± : ¡°Menu¡± }property descriptor
§°§Ò§ì§ñ§Ó§Ý§Ö§ß§Ú§ÖObject.defineProperties({},{class:{value:"Menu",writable:false,configurable:false},height:{value:200,configurable:false}})=> { ¡°class¡± : ¡°Menu¡±, ¡°height¡±: 200 }
§©§Ñ§Ü§â§í§ä§Ú§Ö §à§Ò§ì§Ö§Ü§ä§Ñvaruser={name:"§£§Ñ§ã§ñ",/* ... */}Object.preventExtensions(user)user.a=5// §¯§Ö§Ý§î§Ù§ñ §Õ§à§Ò§Ñ§Ó§Ý§ñ§ä§î §ã§Ó§à§Û§ã§ä§Ó§ÑObject.seal(user)deleteuser.name// §¯§Ö§Ý§î§Ù§ñ §å§Õ§Ñ§Ý§ñ§ä§î §ã§Ó§à§Û§ã§ä§Ó§ÑObject.freeze(user)user.name='§±§Ö§ä§ñ'// §¯§Ö§Ý§î§Ù§ñ §Þ§Ö§ß§ñ§ä§î §ã§Ó§à§Û§ã§ä§Ó§Ñ
§¯§Ñ§ã§Ý§Ö§Õ§à§Ó§Ñ§ß§Ú§Öanimal={canWalk:true}rabbit=Object.create(animal,{canRun:{value:true}})alert(rabbit.canWalk)// trueObject.getPrototypeOf(rabbit) == animal // true
§¯§Ñ§ã§Ý§Ö§Õ§à§Ó§Ñ§ß§Ú§Ö°ù²¹²ú²ú¾±³Ù=°¿²úÂá±ð³¦³Ù.³¦°ù±ð²¹³Ù±ð(²¹²Ô¾±³¾²¹±ô,µ÷³¦²¹²Ô¸é³Ü²Ô:µ÷±¹²¹±ô³Ü±ð:³Ù°ù³Ü±ð°¨°¨)²ú¾±°ù»å=°¿²úÂá±ð³¦³Ù.³¦°ù±ð²¹³Ù±ð(°¿²úÂá±ð³¦³Ù.²µ±ð³Ù±Ê°ù´Ç³Ù´Ç³Ù²â±è±ð°¿´Ú(°ù²¹²ú²ú¾±³Ù),µ÷³¦²¹²Ô¹ó±ô²â:µ÷±¹²¹±ô³Ü±ð:³Ù°ù³Ü±ð°¨°¨)
§¤§Ö§ä§ä§Ö§â§í §Ú §³§Ö§ä§ä§Ö§â§íuser=Object.defineProperty({},"fullName",{get:function(){returnthis.firstName+' '+this.lastName},set:function(value){vars=value.trim().split(/+/,2)this.firstName=s[0]; this.lastName=s[1]}})?user.fullName="§£§Ñ§ã§ñ §±§å§á§Ü§Ú§ß"alert(user.lastName)// §±§å§á§Ü§Ú§ß
§¤§Ö§ä§ä§Ö§â§í §Ú §³§Ö§ä§ä§Ö§â§ívaruser={getfullName(){returnthis.firstName+' '+this.lastName},setfullName(value){vars=value.trim().split(/+/,2)this.firstName=s[0]; this.lastName=s[1]}}?user.fullName="§£§Ñ§ã§ñ §±§å§á§Ü§Ú§ß"alert(user.lastName)// §±§å§á§Ü§Ú§ß
JSON
JSONevent={title:"Conference",date:¡°today"}str=JSON.stringify(event){"title":"Conference","date":"today"}event =JSON.parse(str)@see https://github.com/douglascrockford/JSON-js
JSON ¨C §Ý§ð§Ò§í§Ö §à§Ò§ì§Ö§Ü§ä§í´Ú³Ü²Ô³¦³Ù¾±´Ç²Ô¸é´Ç´Ç³¾(²Ô³Ü³¾²ú±ð°ù)µ÷³Ù³ó¾±²õ.³Ù´Ç´³³§°¿±·=´Ú³Ü²Ô³¦³Ù¾±´Ç²Ô()µ÷°ù±ð³Ù³Ü°ù²Ô²Ô³Ü³¾²ú±ð°ù°¨°¨±ð±¹±ð²Ô³Ù=µ÷³Ù¾±³Ù±ô±ð:&±ç³Ü´Ç³Ù;°ä´Ç²Ô´Ú±ð°ù±ð²Ô³¦±ð&±ç³Ü´Ç³Ù;,»å²¹³Ù±ð:²Ô±ð·É¶Ù²¹³Ù±ð(),°ù´Ç´Ç³¾:²Ô±ð·É¸é´Ç´Ç³¾(22)°¨´³³§°¿±·.²õ³Ù°ù¾±²Ô²µ¾±´Ú²â(±ð±¹±ð²Ô³Ù)µ÷&±ç³Ü´Ç³Ù;³Ù¾±³Ù±ô±ð&±ç³Ü´Ç³Ù;:&±ç³Ü´Ç³Ù;°ä´Ç²Ô´Ú±ð°ù±ð²Ô³¦±ð&±ç³Ü´Ç³Ù;,&±ç³Ü´Ç³Ù;»å²¹³Ù±ð&±ç³Ü´Ç³Ù;:&±ç³Ü´Ç³Ù;2011-02-15°Õ09:12:06.836´Ü&±ç³Ü´Ç³Ù;,&±ç³Ü´Ç³Ù;°ù´Ç´Ç³¾&±ç³Ü´Ç³Ù;:22°¨
JSON ¨C §Ý§ð§Ò§í§Ö §à§Ò§ì§Ö§Ü§ä§ífunctionRoom(number){this.toJSON=function(){returnnumber}}event={title:"Conference",date:newDate(),     Date.prototype.toJSONroom:newRoom(22)}JSON.stringify(event){"title":"Conference","date":"2011-02-15T09:12:06.836Z","room":22}
JSON.stringify(str, whitelist)event={title:"Conference",date:newDate(),domElement:document.body}JSON.stringify(event)=> TypeError: Converting circular structure to JSONJSON.stringify(event,["title","date"])=> {"title":"Conference","date":"2011-02-15T09:44:13.419Z"}
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"}
JSON.parse(str)str='{"title":"Conference",    br />  "date":"2011-02-15T09:44:13.419Z"}'event=JSON.parse(str)§á§â§à§Ò§Ö§Ý§í
JSON.parse(str)str='{"title":"Conference",    br />  "date":"2011-02-15T09:44:13.419Z"}'event=JSON.parse(str)event.date.getDay()=> TypeError: no method 'getDay'
JSON.parse(str, reviver)str='{"title":"Conference",    br />  "date":"2011-02-15T09:44:13.419Z"}'event=JSON.parse(str,function(key,value){if(key=='date'){returnnewDate(value)}returnvalue})event.date.getDay()=> 2
bind
bind(this)functionButton(elem){this.sayHi=function(){alert('Hi')}elem.onclick=function(){this.sayHi()}.bind(this)}
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
Strict mode
use strict"use strict"... code ...
use strictfunctionF(){"use strict"this.method=function(){// strict mode inherited}}
use strictalert(010)// SyntaxError (octal literals deprecated)a=5// ReferenceError (undeclared a)obj.notWritable=...// TypeErrordeleteobj.notConfigurable// TypeErroreval("var a = 5")alert(a)// ReferenceError (undeclared a)arguments.callee// TypeErrorarguments.caller// TypeError(function(){alert(this)// undefined §Ó§Þ§Ö§ã§ä§à window})()with(..)// SyntaxError, 'with' statement
§¶§å§ß§Ü§è§Ú§Ú,§Ü§à§ä§à§â§í§Ö §Õ§Ñ§Ó§ß§à §Ø§Õ§Ñ§Ý§ÚObject.keys(obj)"String".trim()Array.isArray(arr)[...].indexOf/lastIndexOf[...].forEach[...].map[...].filter[...].reduce/reduceRight// ...@seehttp://kangax.github.com/es5-compat-table/
The future is now ?PrototypeJSES5-shim
Harmony
It¡¯s all real__noSuchMethod__Proxy.createletblock_scoped="yay!"constREALLY="srsly"#(x) { x * x }ifx>zreturn"§Ò§Ö§Ù §ã§Ü§à§Ò§à§Ü"moduleIter="@std:Iteration" return[i*iforiinrange(n)]functionprintf(format,...args)ek_scoped= "yay!" consEALLY= "srsly"

More Related Content

EcmaScript 5.1