2013-07-19 34 views
1

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

多一點解釋:

正如你可以在示例代碼中看到的,我將永遠有兩個X1X2一個commonFunction()

我想告訴你X1X2有他們自己的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!"); 
} 

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

回答

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

當在達到最終結果之前上下文變得未定義時會發生什麼?編輯:我看到這正是你的小提琴中發生的事情。 – Adam

+0

@亞當我很抱歉,我不瞭解你。你能解釋我嗎? –

+0

仍然在構造函數體內設置原型,這是錯誤的。你有什麼想法是什麼樣的原型? http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711檢查對象有什麼屬性可以使用'for(something in myObject)'hasOwnProperty'並且上去構造函數' myObject = myObject.constructor'直到你到達Function。但是現在你繼承的方式是銷燬對象原型的構造函數屬性。 – HMR

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 
+0

記住我說:「要注意到,第一手是很重要的我不知道我會打電話給哪個功能,這就是爲什麼我需要一種動態的方式來收集這些信息。「你正在顯式檢查FunctionX2 –

+1

我很清楚你正在構造你的程序是錯誤的。我的意思是,僅僅通過你單獨在JavaScript中編寫類的方式來看。看起來好像你對某個地方的基本面有誤解。我願意爲您提供幫助,但如何解決上述問題而不是修復代碼,讓我們先看看您的目標是什麼。以下是一個例子:不要問:「我怎麼用一個浴室秤來稱量一頭大象?」問:「我怎樣才能確定我的寵物大象不超重?」 – naomik

+0

區別在於:問你應該如何實現你的目標,而不是如何實現你認爲能夠實現你的目標的解決方案。前者爲許多可能的解決方案打開了大門,而後者則將您歸入單一解決方案,從一開始就可能存在缺陷。 – naomik