"Subclassing" basic types in DOM - best method?

Rick Waldron waldron.rick at gmail.com
Tue Nov 20 14:52:46 PST 2012



On Tuesday, November 20, 2012 at 5:23 PM, Tab Atkins Jr. wrote:

> On Tue, Nov 20, 2012 at 2:02 PM, Rick Waldron <waldron.rick at gmail.com> wrote:
> > On Tue, Nov 20, 2012 at 4:52 PM, Tab Atkins Jr. <jackalmage at gmail.com>
> > wrote:
> > > Of course, that pattern is broken too - it lets you detect actual
> > > Arrays, but not things that subclass Array.
> > > 
> > 
> > 
> > That's not possible in JavaScript today, so it's not "broken". There is no
> > way to correctly subclass Array, so this has never been an issue that
> > JavaScript programs would encounter. Future hostile? Maybe.
> > 
> 
> 
> function foo() {...}
> foo.prototype = [];
> 
var a = new foo();
a.push(1); 

a.length; // 1
a.length = 0;

console.log(a);

{"0":1,"length":0}

Again, it's not possible to subclass Array.

Rick


> You get an object which has a self-updating .length property, gets all
> the Array methods like .map(), etc. If that's not "subclassing", I
> don't know what is. ^_^
> 
> ~TJ 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121120/9283d43e/attachment.html>


More information about the es-discuss mailing list