Forum Moderators: open
If you are not using a function literal, then make sure that the function has been defined prior to assignment.
[pre]
function hello()
{
alert(this.message);
}[blue]// A[/blue]
adni = new Object();
adni.message = "I'm Adni";
adni.hi = hello;
adni.bye = function(){ alert("I'm off now.")}
[blue]
//
//* or B object literal (aka 'initializer')[/blue]
adni =
{
message : "I'm Adni",
hi : hello,
bye : function(){ alert("I'm off now.")}
}
[/pre]
Do it this way, and all objects will have the method:
[pre]Object.prototype.hi = hello;
bernard = { message:'Good Morning'}
bernard.hi();[/pre]
In IE, all Javascript objects will have the method.
In Moz, ALL objects will have the method.
You can also override the toString method, which can be handy - especially for debugging.