How can I check If a prototyped function exists?
A little more explanation:
As you can see in the sample code, I will always have a commonFunction()
for both X1
and X2
.
I would like to tell if X1
and X2
have their own myOwnFunction()
.
It's important to notice that at first hand I don't know which function I will be calling. That's why I need a dynamic way to gather that information.
CODE:
function FunctionMain (){};
FunctionMain.FunctionSub = new FunctionSub();
function FunctionX1()
{
FunctionX1.prototype.commonFunction = function()
{
console.log("Hello, I'm X1");
}
FunctionX1.prototype.myOwnFunctionX1 = function()
{
console.log("This my own function");
}
}
function FunctionX2()
{
FunctionX2.prototype.commonFunction = function()
{
console.log("Hello, I'm X2");
}
//I don't have myOwnFunctionX2()
}
function FunctionSub()
{
FunctionSub.prototype.FunctionX1 = new FunctionX1();
FunctionSub.prototype.FunctionX2 = new FunctionX2();
}
//This call works!
FunctionMain.FunctionSub.FunctionX1.commonFunction();
FunctionMain.FunctionSub.FunctionX2.commonFunction();
//what kind of test should I use?
if(typeof "FunctionMain.FunctionSub.FunctionX1.myOwnFunctionX1" == "function")
{
console.log("It exists!");
}
if(typeof window["FunctionMain.FunctionSub.FunctionX1.myOwnFunctionX1"] == "function")
{
console.log("It exists!");
}