MASTERING NAMESPACES! Илья Кантор EcmaScript 5
История
1997 ECMA st edition 1999 ECMA rd edition 2009 ECMA th edition ??? 2001 Compact Profile 2004 E4X var user = alert( == ) — ECMA th ed. — ECMA-262 Harmony
Что нового? ECMA th edition
Багфиксы ECMA th edition
obj = { class: 'Menu' } ES3: ошибка ES5: ok
function test(str) { var re = /ok/g alert( re.test(str) ) } test("ok") // true // ES3: false, ES5: true
alert( parseInt("010") == parseFloat("010") ) ES3: false ES5: true
var obj = { a: 1, b: 2, } var arr [ 1, 2, 3, ] ES3: error ES5: ok
МетаСвойства
writable = false obj = { class: 'Menu' } obj.class = 'Bird‘ => obj.class == ‘Menu’
configurable = false obj = { class: 'Menu' } => obj.class == ‘Menu’ delete obj.class
enumerable = false Object.prototype.each =... for(prop in {}) { // без свойства ‘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 }
Закрытие объекта var user = { name: "Вася", /*... */ } Object.preventExtensions(user) Object.seal(user) Object.freeze(user) user.a = 5 // Нельзя добавлять свойства delete user.name // Нельзя удалять свойства user.name = 'Петя' // Нельзя менять свойства
Наследование animal = { canWalk: true } rabbit = Object.create(animal, { canRun: { value: true } }) alert(rabbit.canWalk) // true Object.getPrototypeOf(rabbit) == animal // true
Наследование rabbit = Object.create(animal, { canRun: { value: true } }) bird = Object.create(Object.getPrototypeOf(rabbit), { canFly: { value: true } })
Геттеры и Сеттеры user = Object.defineProperty({}, "fullName", { get: function() { return this.firstName+' '+this.lastName }, set: function(value) { var s = value.trim().split(/\s+/, 2) this.firstName = s[0]; this.lastName = s[1] } }) user.fullName = "Вася Пупкин" alert(user.lastName) // Пупкин
Геттеры и Сеттеры var user = { get fullName () { return this.firstName+' '+this.lastName }, set fullName (value) { var s = value.trim().split(/\s+/, 2) this.firstName = s[0]; this.lastName = s[1] } user.fullName = "Вася Пупкин" alert(user.lastName) // Пупкин
JSON
event = { title: "Conference", date: “today" } str = JSON.stringify(event) {"title":"Conference","date":"today"} event =
JSON – любые объекты function Room(number) { this.toJSON = function() { return number } event = { title: "Conference", date: new Date(), room: new Room(22) } JSON.stringify(event) {"title":"Conference","date":" T09:12:06.836Z","room":22}
JSON – любые объекты function Room(number) { this.toJSON = function() { return number } event = { title: "Conference", date: new Date(), Date.prototype.toJSON room: new Room(22) } JSON.stringify(event) {"title":"Conference","date":" T09:12:06.836Z","room":22}
JSON.stringify(str, whitelist) event = { title: "Conference", date: new Date(), domElement: document.body } JSON.stringify(event, ["title", "date"]) => {"title":"Conference","date":" T09:44:13.419Z"} JSON.stringify(event) => TypeError: Converting circular structure to JSON
JSON.stringify(str, replacer) event = { title: "Conference", date: new Date(), domElement: document.body } JSON.stringify(event, function(key, value) { return value.nodeName ? undefined : value }) => {"title":"Conference","date":" T09:44:13.419Z"}
JSON.parse(str) str = '{"title":"Conference", \ "date":" T09:44:13.419Z"}' event = JSON.parse(str) пробелы
JSON.parse(str) str = '{"title":"Conference", \ "date":" T09:44:13.419Z"}' event = JSON.parse(str) event.date.getDay() => TypeError: no method 'getDay'
JSON.parse(str, reviver) str = '{"title":"Conference", \ "date":" T09:44:13.419Z"}' event = JSON.parse(str, function(key, value) { if (key == 'date') { return new Date(value) } return value }) event.date.getDay() => 2
bind
bind(this) function Button(elem) { this.sayHi = function() { alert('Hi') } elem.onclick = function() { this.sayHi() }.bind(this) }
bind(this, args) function Button(elem) { this.say = function(phrase) { alert(phrase) } elem.onclick = function(event, phrase) { this.say(phrase) }.bind(this, 'Hi')
Strict mode
"use strict"... code... use strict
function F() { "use strict" this.method = function() { // strict mode inherited } use strict
alert(010) // SyntaxError (octal literals deprecated) use strict a = 5 // ReferenceError (undeclared a) obj.notWritable =... // TypeError delete obj.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
Функции, которые давно ждали Object.keys(obj) "String".trim() Array.isArray(arr) [...].indexOf / lastIndexOf [...].forEach [...].map [...].filter [...].reduce / reduceRight
Harmony
__noSuchMethod__ Proxy.create let block_scoped = "yay!" const REALLY = "srsly" #(x) { x * x } if x > z return "без скобок" module Iter = return [i * i for i in range(n)] function printf(format,...args) ek_scoped = "yay!" consEALLY = "srsly" It’s all real