4 ms·
Has anyone digested this to summarize the significant changes/improvements?
by gord 18y ago
Has anyone digested this to summarize the significant changes/improvements?
- halo 18y agoA quick look. I may have got a few details wrong, since the standard is rather terse: JSON support (adds Internaltype.prototype.toJSON, JSON.parse and JSON.stringify). Object.create now exists, allowing you to create an object from another object's prototypes and, optionally, a set of properties (i.e. equivalent to var constructor = { }; var blah = new constructor(); blah.prototype = originalobject.prototype;). JavaScript has been crying out for this for years. There's also ways of preventing modifications to objects (Object.preventExtensions, Object.seal, Object.freeze) and ways to check them (Object.isFrozen, Object.isExtensible and Object.isFrozen). Data objects have new properties: writable (i.e. whether something is read only), enumerable (i.e. whether it appears in a for-in loop) and configurable (i.e. whether deletable). It now exposes most internal properties of objects via defineProperty/defineProperties, getOwnPropertyNames, getOwnPropertyDescriptor and hasOwnProperty. I think this is what it does anyway: it seems a little subtle when combined with the interaction of prototypes. One subtlety is that it provides syntax for interaction with the properties of HTML elements. I believe it has warm Microsoft support and IE8 already implements large parts of this standard, so it's going to be usable sometime in the future.
- gord 18y agothanks halo for the overview. offtopic - you might be interested in dmitris Raphael Javascript lib to manipulate 2D graphics [uses SVG if its supported, or VML on M$ browsers] - http://raphaeljs.com http://raphaeljs.com
- halo 18y agoI messed up my code example for Object.create above rather badly (I haven't done JS in a while), so I'll redo it here: var constructor = function() { }; originalobject = new constructor(); originalobject.prototype = ...; blah = new constructor(); blah = foo.prototype In ES5 this can be replaced with: var constructor = function() { }; originalobject = new constructor(); originalobject.prototype = ...; boom = Object.create(originalobject); It's worth adding that the ES5 standard implements getters and setters, although I don't believe anything implements it yet (IE8 implements a subset but only for the global and HTML objects apparently): var foo = {}; Object.defineProperty(foo, "bar", { set: function(arg) { alert(arg) } }); //replace the built-in "set" foo.bar = "test"; //alert appears printing "test". Object.defineProperty(foo, "bar", { set: undefined }); //undo our overloading foo.bar = "test"; //sets foo.bar to test, as normal
- anamax 18y ago> enumerable (i.e. whether it appears in a for-in loop Huh? I can see "can appear in a for-in loop", but "appears" is different and I'm not sure why I care or what it even means means. (Appears when is one question.)
- halo 18y agoI meant "whether it can appear". Apologies for any confusion.
- olavk 18y agoAlso: A bunch of higher order functions for arrays: map, reduce, forEach, every, some. Some small syntactical improvements, like the ability to use a trailing comma in list literals [1,2,3,] Getters and setters. A new "strict mode", which disallows some of the more error-prone features. For example, in strict mode, assigning to a non-existing variable throws an error rather than creating a new global variable.