简体   繁体   English

检查javascript中是否存在函数

[英]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 . 正如您在示例代码中看到的,我将始终为X1X2提供commonFunction()

I would like to tell if X1 and X2 have their own myOwnFunction() . 我想告诉X1X2是否有自己的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/ 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 ) 检查这个jsbin (不知道为什么这在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!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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