Approach of new Object methods in ES5

Garrett Smith dhtmlkitchen at gmail.com
Mon Apr 19 23:42:40 PDT 2010


On Sat, Apr 17, 2010 at 12:34 PM, Asen Bozhilov <asen.bozhilov at gmail.com> wrote:
> Erik Arvidsson wrote:
>>
>> Unfortunately there are use case (although limited) that cannot be solved
>> without a mutable __proto__. Extending built *classes* is one such use case.
>>
>> function HelloElement() {
>>  var el = document.createElement('div');
>>  el.__proto__ = HelloElement.prototype;
>>  el.text = 'Hello';
>>  return el;
>> }
>> HelloElement.prototype = {
>>  __proto__: HTMLDivElement.prototype,
>>  set text(text) { this.textContent = text; },
>>  say: function() {
>>    alert('Hello');
>>  }
>> };
>>
>> document.body.appendChild(new HelloElement).say();
>
> That design is used by some js libraries. Mutable __proto__ allow us to
> change prototype chain of created object and safe internal methods of that
> object. For example:
>
> function f() {}
> f.__proto__ = {
>   __proto__ : Function.prototype
> };
>
> f(); //still have [[Call]] method
> new f(); //still have [[Construct]] method
>
> FuseJS use design like that for augment native objects.
> http://github.com/jdalton/fusejs

They really go all-out with that, don't they?

But for what reason? I really don't see the point in all that. Then
again, I don't see the point in parsing the userAgent string for MSIE,
but they do that, too, and apparently something related to somebody
else defining an attachEvent.

To me, that reminds me of the common question "How do I subclass array
in javascript," to which the best answer is "don't." However it looks
as if FuseJS is going to prove that it is possible to provide a
different answer to that old question.

Garrett


More information about the es-discuss mailing list