ECMAScript 3.1 static object methods prototype
While writing the section on ECMAScript 3.1 for my upcoming book, Professional JavaScript, 2nd Edition, I found it useful to create some of the static object methods to play around with. For those unaware, ECMAScript 3.1 defines several methods on Object designed to make it easier to manage object properties. These methods can be used to define new properties, including properties that are enumerable, read only, or otherwise different from standard developer-defined properties. From reading the specification, it’s a little bit difficult to determine how one would use the methods, so I figured I’d create as many as possible using existing ECMAScript 3.0 functionality to make sure I completely understood the functionality. The result is a small library that has basic versions of the following ECMAScript 3.1 static object methods:
Object.create()
– basic functionality works in all browsers. Non-IE browsers allow defining getters and setters. No browsers can defineenumerable
,flexible
, andwritable
on properties as this functionality isn’t available in today’s browsers.Object.clone()
– basic functionality works in all browsers.Object.defineProperty()
– same limitations asObject.create()
.Object.defineProperties()
– same limitations asObject.create()
.Object.getPrototypeOf()
– possibly inaccurate in IE due to lack of__proto__
support.Object.getOwnPropertyNames()
– won’t return non-enumerable properties.Object.getOwnPropertyDescriptor()
–enumerable
,flexible
, andwritable
are always set to true. IE can’t retrieve getters and setters.Object.keys()
– works as expected.
Several of the static methods can’t be implemented using current technology, so I didn’t even bother trying. Therefore, the following six methods aren’t included:
Object.freeze()
Object.preventExtensions()
Object.seal()
Object.isFrozen()
Object.isExtensible()
Object.isSealed()
You can download the source of my static object library along with some examples of usage here. The library isn’t recommended for production usage but may be useful if you want to play with the functionality to see what’s coming down the road. If you’d like to learn more about the static object methods, please refer to this document: Proposed ECMAScript 3.1 Static Object Functions: Use Cases and Rationale.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.