View Full Version : Object methods, check if they exist & how to call dynamically?

Mar 18th, 2007, 03:34 AM
Hi, decided to turn to objects for my validation script ... in the below
function farm() is supposed to see if a method exists for the given type & if so then call it, so if obj.vType = 'vtext' then I want to call the obj.vtext() method.. alternatively (and this is not important right now) I would like to also check if an external function vtext exists if the internal method does not.

so if this were PHP I would do something like this...

$tmp = $this->vType;//might translate to 'vtext'
return $this->$tmp();
}else{//a function external to this class
return $tmp();

so how would I do similar in the farm() method below?

function textarea(){
//ths function exists though outside of the object

function vObj(vId, vType, vMin, vMax, vRegex, vStr){

function getType() {
return document.getElementById(this.vId).type;
this.getType = getType;

//farm out to correct validation type by element type
function farm(){
if(typeof this.vType == 'function') {
alert(this.vType+' is covered');
alert(this.vType+ ' not covered');
this.farm = farm;
//validate text type
function vtext(){
//this method exists

PS, I know there are other ways to create javascript methods, I would rather stick to the above for now if possible until I get a grasp on them.

Mar 18th, 2007, 07:16 AM
if ("vtext" in obj && typeof obj.vtext == "function")

for instance...

A method is just a property value (as functions are first-class datatypes), so there is no difference in checking for the existence of a method and checking for any old property.

Mar 18th, 2007, 08:39 AM
ok, cool , with you on that ... but I want to do this dynamically .. so for instance the following snippet ..

case 'text' : val = this.get_vtext();break;
case 'textarea' : val = this.get_vtextarea();break;
case 'select-one' : val = this.get_vselectOne();break;
case 'select-multiple' : val = this.get_vselectMultiple();break;
case 'password' : val = this.get_vtext();break;
case 'hidden' : val = this.get_vtext();break;
case 'radio' : val = this.get_vradio();break;
case 'checkbox' : val = this.get_vcheckbox();break;
default : alert('input method not supported');
//validate val ...

relies on ....
1)passing the element type to the object (& since I have the id I can fetch that via javascript anyway) &
2)a nasty long switch which I am sure is not required IF I could do this...

if (elType in obj && typeof obj.elType == "function"){
//check for external functions
alert('Input not implemented');

in PHP the following would work ...
but in javascript it does not.. I assume that eval() might come inti play here somewhere, but as usual I would like to avoid eval() if possible.

any ideas ?

errr just realized that this would require functions called 'text', 'select-one' etc, is that an issue ? or can I prepend the function name in someway ?

Mar 18th, 2007, 08:58 AM
You may use JSON


for instance, your case, try:


Mar 18th, 2007, 06:34 PM
It looks like you have plenty of good help but I'll take a shot at it too for fun. :) The only issue is the hyphen to camelCase you have in your switch statement (like "select-one" to "selectOne"). If you really need this functionality though it wouldn't be hard to write a converter.

var getFunc = "get_v" + type.vEtype;
if (getFunc in this && typeof this[getFunc] == "function") {
(this[getFunc])(); // i'm not positive you need the extra paren, but it can't hurt
} else if (isExternal(this.vEtype)) {
/* do external */
} else {
alert("Error: unknown vEtype = '" + this.vEtype "'");