Here’s my code:
Image.prototype.x = 0;
Image.prototype.y = 0;
var blankImage = new Image();
blankImage.src = "blank.png";
blankImage.x = 16;
blankImage.y = 16;
In Firefox if I do blankImage.x it will return 16, but in Chrome it returns 0. Is there any way around this? I like this solution for my current problem..
Chrome already defines
xandyon image elements. Since you haven’t inserted the element into the page, itsxandywill be0.Run this in Chrome’s Console on a page with at least one image…
You will see that the
HTMLImageElementalready has that property directly on it, not on itsprototypeobject.You can also see it visually if you enter this in your console…
…and then expand the object.
The
xandyproperties are immutable can’t be changed, hence they retain their value when you attempt to modify them (as if theirwritableisfalse).If you were to do the same with a property that doesn’t already exist on a
HTMLImageElement, it would work as you expected.