JavaScript subclassing

written by Tobie on February 8th, 2007 @ 12:30 PM

Am I doing something wrong or is this somehow broken?

function ExtendedString() {};

ExtendedString.prototype = new String;

var str = new ExtendedString('some text.')
alert(str.length);
// -> 0 in Safari and IE
// -> an error in Firefox 
// (String.prototype.toString called on incompatible Object)

try it!

Comments

  • John Resig
    John Resig says:

    IE doesn’t allow you to “sub-class” native objects. The same thing happens if you try to overwrite Array, too. (In this case, a similar problem must be happening in the other browsers.)

    Thu, February 08 at 13:21 PM

  • Rich Waters
    Rich Waters says:

    http://dean.edwards.name/weblog/2006/11/hooray/

    Thu, February 08 at 13:45 PM

Comments are closed