简体   繁体   中英

check if function exists in javascript

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

FIDDLE: http://jsfiddle.net/matias/FTzjW/

This is weird, don't do this

function FunctionX2()
{
    FunctionX2.prototype.commonFunction = function()
    {
        console.log("Hello, I'm X2");
    }

    //I don't have myOwnFunctionX2()
}

Do this instead

var FunctionX2 = function() {
  // constructor
};

FunctionX2.prototype.commonFunction = function() {
  console.log("Hello, I'm X2");
};

Check if it exists directly

typeof FunctionX2.prototype.commonFunction === 'function';
// => true

Or with an instance

var f2 = new FunctionX2();
typeof f2.commonFunction === 'function';
// => true

Here's a demonstration that checking for the function dynamically is possible

var functionExists = function(receiver, functionName) {
  return typeof receiver[functionName] === 'function';
};

var commonFunctionExists = function(receiver) {
  return functionExists(receiver, 'commonFunction');
};

Some tests

var f1 = new FunctionX1();
commonFunctionExists(f1);
// => true

var f2 = new FunctionX2();
commonFunctionExists(f2);
// => true

var obj = new Object();
commonFunctionExists(obj);
// => false

This is the solution that worked for me.

Check this jsbin (don't know why this doesn't work in jsfiddle )

FULL 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();


//use this test
function testFunction(function_to_find)
{
    var context = window;
    var functions = function_to_find.split(".");
    var method = functions.pop();

    for (var i = 0; i < functions.length; i++)
    {
        context = context[functions[i]];
    }

    return typeof context[method];
}

if(testFunction("FunctionMain.FunctionSub.FunctionX1.myOwnFunctionX1") == "function") console.log("yes x1!");

if(testFunction("FunctionMain.FunctionSub.FunctionX2.myOwnFunctionX2") == "function") console.log("yes x2!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM