View Full Version : analyzing a different object whether the method exist

06-07-2007, 05:30 PM
hi all,
basically im trying to have a function of that form

function OB(element) {
/* analyses what method are sent to OB(element) and return a different object */

so that i could write:


and it would run:

try {
var returnvalue = objects[element].doThis();
return returnvalue;
} catch(e) {
var returnvalue = otherObjects[element].doThis();
return returnvalue;

so it would return either
objects[element].doThis(); or
otherObjects[element].doThis(); depending on whether the function exists or not.

Any idea on how to do it??

06-07-2007, 06:18 PM
I'm not quite sure that I follow your example, but this might help.

If you want to see if a function exists on an object, use this method:

function isFunction(o) {return 'function' == typeof o;}

in conjunction with

if (isFunction(myObject.doThis)) {
// execute code when function exists
else {
// execute code when function does not exist

06-07-2007, 08:24 PM
that's precisely what i'd like do ,
but as im trying to keep everything as OOP as i can,
i wanted to implement that method within a class itself, so that i could run


where doThis isnt really a method for OB, but for two possibilities returned by OB(element)

then if array1[element].doThis exists, it executes it,
otherwise array2[element].doThis would be executed...

06-14-2007, 02:41 AM
here is an article that compares two different OOP approaches used by the Prototype and YUI Frameworks.

Prototype vs. YUI round 1: OOP Architecture (http://mattsnider.com/?p=14)

Hopefully, this helps.