Download - ECMAScript 5: Новое в JavaScript
Syntax
Reserved words
var label = {
class : "active"
}
Comma
var fibonacci = [0, 1, 2, 3, 5, ];
Line breaks
var literal = "Line breaks \
is standardized";
Arrays
Iterations
forEach(), every(), some(),map(), filter(),reduce(), reduceRight(),
SearchindexOf(), lastIndexOf()
Type checkfibonacci.isArray() === true;
JSON
var event = {title : "HTML5Camp ",date : new Date("2011-03-28T10:00:00.000Z"),
}
JSON.stringify(value, replacer, space)
var str = JSON.stringify(event);{"title":"HTML5Camp","date":"2011-03-28T10:00:00.000Z"}
JSON.parse(string, reviver)JSON.parse(str, function(key, value){
if(key == "title"){return value.trim();
}});
Strict mode
"use strict";
location = "http://www.microsoft.com";
var person = {"name" : "Yura", "name" : "Chaikovsky"};
var geval = eval;
with, arguments.caller, arguments.callee
(function(){...}).call(null);
(function(){return !this;
})();
Function.prototype.bind(thisArg, arg1, arg2....)
Objects: Point
function Point(x, y) { this.x = +x; this.y = +y; }
var pt = new Point(3,5);
pt.x = 'foo';
Objects: Creating
Object.create(p,{(n:attrs)*}) .defineProperty(o,n,attrs)
.defineProperties(o,{(n:attrs)*}) .getOwnPropertyNames(o) → names .getOwnPropertyDescriptor(o,n) → attrs
attrs ::= { value:v, writable:b, enumerable:b, configurable:b } |
{ get:f()→v, set:f(v),
enumerable:b, configurable:b }
Objects: New Point
function Point(x, y) { return Object.freeze({ x: +x, y: +y });
}
// (new Point(3,4) instanceof Point) === false
Objects: New Point
function Point(x, y) { return Object.freeze({ x: +x, y: +y });
}
// (new Point(3,4) instanceof Point) === false
function Point(x, y) { return Object.freeze( Object.create(Point.prototype, { x: { value: +x, enumerable: true }, y: { value: +y, enumerable: true } }));}
Objects: Accessor (getter/setter)
Old school...
var evnt = (function(){var eventType = "barcamp";return {
type : function(tp){if(typeof tp != "undefined"){
//setter...eventType = tp;
}else{//getter...return eventType;
}}
};
})();
Objects: Creating
Object.create(p,{(n:attrs)*}) .defineProperty(o,n,attrs)
.defineProperties(o,{(n:attrs)*}) .getOwnPropertyNames(o) → names .getOwnPropertyDescriptor(o,n) → attrs
attrs ::= { value:v, writable:b, enumerable:b, configurable:b } |
{ get:f()→v, set:f(v),
enumerable:b, configurable:b }
Objects: Accessor (getter/setter)
Brand new...
var evnt = {eventType : "barcamp",
get type(){//getter...return evnt.eventType;
},
set type(tp){//setter...evnt.eventType = tp;
}};
Objects: Accessor (getter/setter)
or...
var evnt = Object.defineProperties({}, {
'typeName' : {value : "barcamp",
},
'type' : { get: function() { return …; }, set: function(tp) { … }, }
});
Objects: Restrictions
Object.preventExtensions( obj )Object.isExtensible( obj )
Object.seal( obj )Object.isSealed( obj )
Object.freeze( obj )Object.isFrozen( obj )
References
Mark Millerhttp://www.youtube.com/watch?v=Kq4FpMe6cRs
Douglas Crockfordhttp://www.slideshare.net/douglascrockford/newandimproved
Ilia Kantorhttp://www.youtube.com/watch?v=082iuEKV72c
John Resighttp://ejohn.org/blog/ecmascript-5-objects-and-properties/http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
David Flanaganhttp://davidflanagan.com/Talks/es5/slides.html