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.create Element('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('mypict ure.jpg');
document.body.a ppendChild(myIm age);
myImage.style.w idth = 300;
myImage.myPrope rty = 'anotherValue';
The problem comes when I then try to define methods on the object's
prototype, e.g.
MyImage.prototy pe.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.
functions) in javascript with HTML elements. For example, I have an
object 'class' MyImage:
function MyImage(file)
{
var img = document.create Element('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('mypict ure.jpg');
document.body.a ppendChild(myIm age);
myImage.style.w idth = 300;
myImage.myPrope rty = 'anotherValue';
The problem comes when I then try to define methods on the object's
prototype, e.g.
MyImage.prototy pe.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.
Comment