Download presentation
Presentation is loading. Please wait.
1
MASTERING NAMESPACES! Илья Кантор http://javascript.ru EcmaScript 5
2
История
3
1997 ECMA-262 1 st edition 1999 ECMA-262 3 rd edition 2009 ECMA-262 5 th edition ??? 2001 Compact Profile 2004 E4X var user = alert( user.field.(@type == "email").@value ) — ECMA-262 4 th ed. — ECMA-262 Harmony
4
Что нового? ECMA-262 5 th edition
5
Багфиксы ECMA-262 5 th edition
6
obj = { class: 'Menu' } ES3: ошибка ES5: ok
7
function test(str) { var re = /ok/g alert( re.test(str) ) } test("ok") // true // ES3: false, ES5: true
8
alert( parseInt("010") == parseFloat("010") ) ES3: false ES5: true
9
var obj = { a: 1, b: 2, } var arr [ 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' } => obj.class == ‘Menu’ delete obj.class
13
enumerable = false Object.prototype.each =... for(prop in {}) { // без свойства ‘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
Закрытие объекта var user = { name: "Вася", /*... */ } Object.preventExtensions(user) Object.seal(user) Object.freeze(user) user.a = 5 // Нельзя добавлять свойства delete user.name // Нельзя удалять свойства 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() { 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) // Пупкин
20
Геттеры и Сеттеры 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) // Пупкин
21
JSON
22
event = { title: "Conference", date: “today" } str = JSON.stringify(event) {"title":"Conference","date":"today"} event = JSON.parse(str) @see https://github.com/douglascrockford/JSON-jshttps://github.com/douglascrockford/JSON-js
23
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":"2011-02-15T09:12:06.836Z","room":22}
24
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":"2011-02-15T09:12:06.836Z","room":22}
25
JSON.stringify(str, whitelist) event = { title: "Conference", date: new Date(), domElement: document.body } JSON.stringify(event, ["title", "date"]) => {"title":"Conference","date":"2011-02-15T09:44:13.419Z"} JSON.stringify(event) => TypeError: Converting circular structure to JSON
26
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":"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') { return new Date(value) } return value }) event.date.getDay() => 2
30
bind
31
bind(this) function Button(elem) { this.sayHi = function() { alert('Hi') } elem.onclick = function() { this.sayHi() }.bind(this) }
32
bind(this, args) function Button(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"... code... use strict
35
function F() { "use strict" this.method = function() { // strict mode inherited } use strict
36
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
37
Функции, которые давно ждали Object.keys(obj) "String".trim() Array.isArray(arr) [...].indexOf / lastIndexOf [...].forEach [...].map [...].filter [...].reduce / reduceRight //... @see http://kangax.github.com/es5-compat-table/http://kangax.github.com/es5-compat-table/
38
Harmony
39
__noSuchMethod__ Proxy.create let block_scoped = "yay!" const REALLY = "srsly" #(x) { x * x } if x > z return "без скобок" module Iter = "@std:Iteration" return [i * i for i in range(n)] function printf(format,...args) ek_scoped = "yay!" consEALLY = "srsly" It’s all real
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.