8/22/2006

Note: Function as Object in Javascript

REF.

...in JS a function is also an object. You can pass a function around as an argument to another function just like you can pass a string, for example. This is extensively used and very handy.

...


var myDog = {
bark: function(){
alert('Woof!');
}
};

var myCat = {
meow: function(){
alert('I am a lazy cat. I will not meow for you.');
}
};

function annoyThePet(petFunction){
//let's see what the pet can do
petFunction();
}

//annoy the dog:
annoyThePet(myDog.bark);
//annoy the cat:
annoyThePet(myCat.meow);

No comments: