redefine.js - A Simplified ES5 Approach

# Andrea Giammarchi (13 years ago)

not only "Security Demands Simplicity", but development demands simplicity too :-)

I don't remember who asked me to show a simplified approach to ES5 descriptors, Object.create, defineProperty, and defineProperties, but here a tiny library which aim is to make developers life easier, the usage of defaults for grouped type of descriptors, and the fact it does not break if some evil code redefine some Object.prototype.writable/configurable property in the global scope.

I'd appreciate any sort of feedback when and if you have time and please do not hesitate to ask me more, if necessary, thanks.

WebReflection/redefine#redefinejs

Best , Andrea Giammarchi

# Andrea Giammarchi (13 years ago)

OK, if you prefer a list of HOWTO that compares redefine.js and ES5 approach, included problems with inheritance, here a more technical face 2 face approach: WebReflection/redefine/blob/master/HOWTO.md

Thanks in advance for any feedback or question about it.

# Andrea Giammarchi (13 years ago)

and the blog entry webreflection.blogspot.com/2013/01/redefinejs-simplified-es5-approach.htmlthen I swear, I won't bother anymore unless somebody will ask anything or give some feedback :-)

# Brendan Eich (13 years ago)

For defining properties, looks nice and concise. The first bit shows no win over Object.create, IMHO.

# Andrea Giammarchi (13 years ago)

thanks, I've posted so many links I am not sure what's the first bit but Object.create(proto[, descriptorsList]) is actually redefine.from(proto[, keyValuesList[, descriptorDefaults]]) so that

var newInstance = Object.create(null, { name: { value: "new Instance" }, toString: { value: function () { return this.name; } } });

would be the exact equivalent of

var newInstance = redefine.from(null, { name: "new Instance", toString: function () { return this.name; } });

So the main win is the ability to use the list of key/values instead of key/descriptors

I also found this approach cleaner to read because of this context which looks correct in redefine while it looks a descriptor context in common ES5 style, i.e.

var genericObject = { get: function () { return Object.keys(this); } };

Using that as descriptor is as legit as using that as stand alone object. In this case the difference would be:

var o = Object.create(null, { keys: { get: function () { return Object.keys(this); } } });

VS

var o = redefine.from(null, { keys: redefine.as({ get: function () { return Object.keys(this); } }) });

Slightly more verbose but more semantic too.

br