簡體   English   中英

檢查javascript中是否存在函數

[英]check if function exists in javascript

如何檢查原型函數是否存在?

再解釋一下:

正如您在示例代碼中看到的,我將始終為X1X2提供commonFunction()

我想告訴X1X2是否有自己的myOwnFunction()

重要的是要注意到,我不知道我將調用哪個函數。 這就是為什么我需要一種動態的方式來收集這些信息。

代碼

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/

這很奇怪,不要這樣做

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

    //I don't have myOwnFunctionX2()
}

這樣做

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

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

檢查它是否直接存在

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

或者使用實例

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

這是一個動態檢查功能的演示

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

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

一些測試

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

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

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

這是對我有用的解決方案。

檢查這個jsbin (不知道為什么這在jsfiddle不起作用

完整代碼

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