PDA

View Full Version : Extending objects IE6-8



Lamped
01-22-2011, 03:28 PM
Good day,

I have the following code:



var img = new Image();
img.myProperty = 'something';


I've tried Image.prototype.myProperty and a few other things. It seems IE just doesn't want me extending the Image object.

Is there a known work-around?

Thanks

mrhoo
01-22-2011, 06:14 PM
I don't have IE6 handy, but this works in IE8, and the other browsers- (I call this on a page that includes an image element)



var IM=new Image();
IM.constructor.prototype.monthname='January';

alert(document.images[0].monthname)


/* returns 'January' */

Lamped
01-22-2011, 06:55 PM
Oooh, not seen "constructor.prototype" before. I'll try it, thanks.

venegal
01-23-2011, 04:03 AM
I highly doubt that that would work even in IE8.

As you can see here (http://msdn.microsoft.com/en-us/library/f5s9ycex(vs.71).aspx)and here (http://msdn.microsoft.com/en-us/library/c1hcx253(v=vs.71).aspx), neither the constructor nor the prototype property apply to Image objects in earlier versions of Internet Explorer.

That's because Image objects are no intrinsic objects in JScript (Microsofts implementation of ECMAScript), with intrinsic objects being only Array, Boolean, Date, Error, Function, Global, JSON, Math, Number, Object, RegExp, Regular Expression, and String.

Starting with IE8, prototpye should work with images (which should make your original code work), but as far as I know you can't even get to the constructor in IE9.