redefine.js - A Simplified ES5 Approach

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun Jan 20 14:06:22 PST 2013


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


On Sun, Jan 20, 2013 at 1:36 PM, Brendan Eich <brendan at mozilla.com> wrote:

> For defining properties, looks nice and concise. The first bit shows no
> win over Object.create, IMHO.
>
> /be
>
> Andrea Giammarchi wrote:
>
>> and the blog entry http://webreflection.blogspot.**
>> com/2013/01/redefinejs-**simplified-es5-approach.html<http://webreflection.blogspot.com/2013/01/redefinejs-simplified-es5-approach.html>then I swear, I won't bother anymore unless somebody will ask anything or
>> give some feedback :-)
>>
>> thanks
>>
>>
>> On Sat, Jan 19, 2013 at 10:41 PM, Andrea Giammarchi <
>> andrea.giammarchi at gmail.com <mailto:andrea.giammarchi@**gmail.com<andrea.giammarchi at gmail.com>>>
>> wrote:
>>
>>     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:
>>     https://github.com/**WebReflection/redefine/blob/**master/HOWTO.md<https://github.com/WebReflection/redefine/blob/master/HOWTO.md>
>>
>>     Thanks in advance for any feedback or question about it.
>>
>>
>>     On Sat, Jan 19, 2013 at 1:33 PM, Andrea Giammarchi
>>     <andrea.giammarchi at gmail.com <mailto:andrea.giammarchi@**gmail.com<andrea.giammarchi at gmail.com>
>> >>
>>
>>     wrote:
>>
>>         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.
>>
>>         https://github.com/**WebReflection/redefine#**redefinejs<https://github.com/WebReflection/redefine#redefinejs>
>>
>>         Best Regards,
>>             Andrea Giammarchi
>>
>>
>>
>> ______________________________**_________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130120/0c33d116/attachment.html>


More information about the es-discuss mailing list