I am having trouble trying to more closely relate objects (as
functions) in javascript with HTML elements. For example, I have an
object 'class' MyImage:
function MyImage(file)
{
var img = document.createElement('IMG');
img.src = file;
img.myProperty = 'myValue';
return img;
}
This uses the DOM to construct and return an HTML IMG element. I can
then instantiate this and refer directly to the img attributes as well
as any properties I have defined, e.g.
var myImage = new MyImage('mypicture.jpg');
document.body.appendChild(myImage);
myImage.style.width = 300;
myImage.myProperty = 'anotherValue';
The problem comes when I then try to define methods on the object's
prototype, e.g.
MyImage.prototype.myMethod = function()
{
alert("shiver in eternal darkness");
}
When called, this gives the error: "Object doesn't support this
property or method".
Is what I am doing by returning a created HTML element from the object
constructor valid and acceptable? If so, how do I define methods on
the object?
Any help would be much appreciated.