我想將函數「testMath」的名稱作爲字符串傳遞給名爲「runTest」的包裝函數作爲參數。然後在'runTest'裏面我會調用傳遞的函數。我這樣做的原因是因爲我們有一組通用數據,無論測試如何,都會將其填充到變量中,然後根據用戶想要測試的內容調用特定的測試。我正在嘗試使用javascript/jquery來做到這一點。事實上,這個函數要複雜得多,包括一些Ajax調用,但是這個場景強調了基本的挑戰。如何將函數的名稱作爲參數傳遞,然後再引用該函數?
//This is the wrapper that will trigger all the tests to be ran
function performMytests(){
runTest("testMath"); //This is the area that I'm not sure is possible
runTest("someOtherTestFunction");
runTest("someOtherTestFunctionA");
runTest("someOtherTestFunctionB");
}
//This is the reusable function that will load generic data and call the function
function runTest(myFunction){
var testQuery = "ABC";
var testResult = "EFG";
myFunction(testQuery, testResult); //This is the area that I'm not sure is possible
}
//each project will have unique tests that they can configure using the standardized data
function testMath(strTestA, strTestB){
//perform some test
}
我可以看到這可能會更清潔,但不幸的是,適用於我的方案不起作用。 – silvster27