如何检查原型函数是否存在?
再解释一下:
正如您在示例代码中看到的那样,commonFunction()
和X1
的{{1}}总是X2
。
我想告诉X1
和X2
是否拥有自己的myOwnFunction()
。
重要的是要注意,我不知道我将调用哪个函数。这就是为什么我需要一种动态的方式来收集这些信息。
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!");
}
答案 0 :(得分:7)
这很奇怪,不要这样做
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
答案 1 :(得分:-3)
这是适合我的解决方案。
选中此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!");