PDA

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



firepages
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...


<?php
$tmp = $this->vType;//might translate to 'vtext'
if(method_exists($this,$tmp)){
return $this->$tmp();
}else{//a function external to this class
if(function_exists($tmp)){
return $tmp();
}
}
?>


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



<script>
function textarea(){
//ths function exists though outside of the object
}

function vObj(vId, vType, vMin, vMax, vRegex, vStr){
this.vId=vId;
this.vType=vType;
this.vMin=vMin;
this.vMax=vMax;
this.vRegex=vRegex;
this.vStr=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');
}else{
alert(this.vType+ ' not covered');
}
}
this.farm = farm;
//validate text type
function vtext(){
//this method exists
}
this.vtext=vtext;
}
</script>


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.

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


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.

firepages
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 ..


switch(this.vEtype){
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 ...
etc


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...

..........................
elType=document.getElementById(this.vId).type;
if (elType in obj && typeof obj.elType == "function"){
obj.elType();
}else{
//check for external functions
}else{
alert('Input not implemented');
}
.........................

in PHP the following would work ...
$this->$elType();
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 ?
obj.'v_'+elType();

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

obj['someattribute']
obj['somefunction']()

for instance, your case, try:

obj['v_'+elType]()

david_kw
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 "'");
}



david_kw