我對js編程非常陌生。我正在爲測試開發工作。我有要求調用一個js函數與存儲到文件的名稱。比如我有兩個文件,javascript調用名稱存儲到變量的方法
file1.sah
//sah is sahi extension but internally the file has javascript code only
function test(){
this.var1 = 100;
this.logFunc = function(a,b,c){
console.log(a + b + c + this.var1);
}
}
file2.sah
include file1.js //file1.js module is referenced
var obj = new test();
var $method = "logFunc";
var $params = {"a" : 1, "b" : 2, "c" : 3};
//wanted to call the method "test" from file1 and pass all arguments as like key & value pair in object
//I cannot use window objects here
eval($method).apply(obj, $params);
//eval works but I couldn't pass the object params I have. For simplicity I //have initialised params in this file. In my real case it will come from a
//different file and I will not know the keys information in the object
該方法的名稱是'logFunc',而不是'test'。 – Barmar