PDA

View Full Version : a function in an object


kwhubby
03-30-2003, 09:18 AM
Hi i am trying to make a function of an object that can be used, such as the random generater function: Math.random() wich is the function random, which is part of the object Math. Ive never needed to do this before so I am just curious how it would be down. I know how to make values of an object like abc = new Object(); abc.asdf = "hello"
Ive tried doing something like abc = new Object(); abc.asdf = new function() { alert("hi") }

but I cant seem to figure out how to be able to make it right since if I were to do abc.asdf() it would give error.
I would appreciate anybody instructing me on how to do this.

ASAAKI
03-30-2003, 10:04 AM
you'll use something like:
something=new abc();

after you've made your 'abc' object like:

fuction abc(){
...
...
/*and inside here is where you're going to define abc's own functions (they're called methods when they belong to an object).
like:
*/
this.asdf = function(){
...
...
}

}

something like that. then you can use your new object like:

something = new abc();

and use its methods like:

something.asdf();

but there's a lot more to it. there's a prototype function or something too...

ASAAKI
03-30-2003, 10:06 AM
this (http://www.javascriptkit.com/javatutors/oopjs.shtml) should be useful.

kwhubby
03-30-2003, 10:23 AM
ok thanks, that was the answer i was looking for, thanks, i never have made a custom object with a function in it before, so I didnt know what the correct syntax would be.