Consider the following code:
if(eform[funcName] !== undefined){
if(eform[funcName].init !== undefined){
//finally do something
}
}
I’m first checking to see if the eform object has the property specified by the variable funcName. If it does, then I need to check whether that property has an init method.
Is there any way to combine these into a single if statement? Or perhaps something even more elegant than that?
Using Short-Circuit evaluation:
If
eform[funcName]is undefined the statement if false andeform[funcName].initis never checked. Depending on preference/readability this following is vaild as well: