मैंने इस समस्या पर काम किया है, क्योंकि मुझे इस तरह के एक समारोह की आवश्यकता है। यहां मेरा सैंडबॉक्स कोड है, पूरी तरह से परीक्षण नहीं किया गया है, लेकिन दूसरों के लिए एक प्रारंभ बिंदु हो सकता है। ध्यान दें कि कोड में एक eval() है क्योंकि मैं यह नहीं समझ सका कि उस चरण को कैसे बाईपास करना है, शायद जावास्क्रिप्ट क्विर्क और किसी भी अन्य तरीके से नहीं किया जा सकता है। अगर मुझे eval() से छुटकारा पाने का कोई तरीका है तो मुझे बताएं!
executeFunctionByName = function(functionName)
{
var args = Array.prototype.slice.call(arguments).splice(1);
//debug
console.log('args:', args);
var namespaces = functionName.split(".");
//debug
console.log('namespaces:', namespaces);
var func = namespaces.pop();
//debug
console.log('func:', func);
ns = namespaces.join('.');
//debug
console.log('namespace:', ns);
if(ns == '')
{
ns = 'window';
}
ns = eval(ns);
//debug
console.log('evaled namespace:', ns);
return ns[func].apply(ns, args);
}
core = {
paragraph: {
titlebar: {
user: "ddd",
getUser: function(name)
{
this.user = name;
return this.user;
}
}
}
}
var testf = function()
{
alert('dkdkdkd');
}
var x = executeFunctionByName('core.paragraph.titlebar.getUser', 'Ikon');
executeFunctionByName('testf');
संभव डुप्लिकेट [एक निष्पादित करने के लिए कैसे जब मेरे पास स्ट्रिंग के रूप में इसका नाम होता है तो जावास्क्रिप्ट फ़ंक्शन] (http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a- स्ट्रिंग) – olibre