检查javascript中是否存在函数

检查javascript中是否存在函数,javascript,function,Javascript,Function,如何检查原型函数是否存在 再解释一下: 正如您在示例代码中看到的,对于X1和X2,我将始终使用commonFunction() 我想告诉大家X1和X2是否有自己的myOwnFunction() 重要的是要注意,第一手我不知道我将调用哪个函数。这就是为什么我需要一种动态的方式来收集这些信息 代码: function FunctionMain (){}; FunctionMain.FunctionSub = new FunctionSub(); function FunctionX1() {

如何检查原型函数是否存在

再解释一下:

正如您在示例代码中看到的,对于
X1
X2
,我将始终使用
commonFunction()

我想告诉大家
X1
X2
是否有自己的
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!");
}
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!");

小提琴:

这很奇怪,不要这样做

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

这很奇怪,不要这样做

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

这就是对我有效的解决方案

检查此项(不知道为什么此项在中不起作用)

完整代码

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!");
}
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!");
函数FunctionMain(){}
FunctionMain.FunctionSub=新FunctionSub();
函数FunctionX1()
{
FunctionX1.prototype.commonFunction=函数()
{
log(“你好,我是X1”);
}
FunctionX1.prototype.myOwnFunctionX1=函数()
{
log(“这是我自己的函数”);
}    
}
函数FunctionX2()
{
FunctionX2.prototype.commonFunction=函数()
{
log(“你好,我是X2”);
}
//我没有myOwnFunctionX2()
}
函数FunctionSub()
{
FunctionSub.prototype.FunctionX1=新函数X1();
FunctionSub.prototype.FunctionX2=新的FunctionX2();
}
//这个电话有效!
FunctionMain.FunctionSub.FunctionX1.commonFunction();
FunctionMain.FunctionSub.FunctionX2.commonFunction();
//使用此测试
函数testFunction(函数查找)
{
var上下文=窗口;
var functions=函数_to _find.split(“.”);
var方法=functions.pop();
对于(var i=0;i
这是对我有效的解决方案

检查此项(不知道为什么此项在中不起作用)

完整代码

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!");
}
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!");
函数FunctionMain(){}
FunctionMain.FunctionSub=新FunctionSub();
函数FunctionX1()
{
FunctionX1.prototype.commonFunction=函数()
{
log(“你好,我是X1”);
}
FunctionX1.prototype.myOwnFunctionX1=函数()
{
log(“这是我自己的函数”);
}    
}
函数FunctionX2()
{
FunctionX2.prototype.commonFunction=函数()
{
log(“你好,我是X2”);
}
//我没有myOwnFunctionX2()
}
函数FunctionSub()
{
FunctionSub.prototype.FunctionX1=新函数X1();
FunctionSub.prototype.FunctionX2=新的FunctionX2();
}
//这个电话有效!
FunctionMain.FunctionSub.FunctionX1.commonFunction();
FunctionMain.FunctionSub.FunctionX2.commonFunction();
//使用此测试
函数testFunction(函数查找)
{
var上下文=窗口;
var functions=函数_to_ufind.split(“.”;
var方法=functions.pop();
对于(var i=0;i
请记住我说过的话:“首先要注意,我不知道要调用哪个函数,这很重要。这就是为什么我需要一种动态的方法来收集这些信息。”。你在明确地检查函数X2,我很清楚你的程序结构是错误的。我的意思是,仅从JavaScript编写类的方式来看。似乎你对某个地方的基本原理有误解。我很愿意帮助您,但与其修改上面的代码,不如让我们先看看您的目标是什么。举个例子:不要问“我如何用浴室磅秤称大象的体重?”而要问“我如何才能确保我的宠物大象不超重?”区别在于:问你应该如何实现你的目标,而不是问你如何实施你认为可以实现你的目标的解决方案。前者为许多可能的解决方案打开了大门,而后者则将您归类为一个可能从一开始就存在缺陷的单一解决方案。为了它的价值,我在回答的末尾添加了一个演示记住我说过的:“重要的是要注意,第一手我不知道我将调用哪个函数。这就是为什么我需要一种动态的方式来收集这些信息“。你在明确地检查函数X2。我很清楚,你的程序结构是错误的。我的意思是,仅从JavaScript编写类的方式来看。似乎你对某个地方的基本原理有误解。我很愿意帮助您,但与其修改上面的代码,不如让我们先看看您的目标是什么。举个例子:不要问“我如何用浴室磅秤称大象的体重?”而要问“我如何才能确保我的宠物大象不超重?”区别在于:问你应该如何实现你的目标,而不是问你如何实施你认为可以实现你的目标的解决方案。前者为许多可能的解决方案打开了大门,而后者则将您放入一个可能从一开始就存在缺陷的单一解决方案中。出于其价值,我在回答what的末尾添加了一个演示