下面是一种更通用的方法,在支持作用域的同时,也可以这样做:
// Get function from string, with or without scopes (by Nicolas Gauthier) window.getFunctionFromString = function(string){ var scope = window; var scopeSplit = string.split('.'); for (i = 0; i < scopeSplit.length - 1; i++) { scope = scope[scopeSplit[i]]; if (scope == undefined) return; } return scope[scopeSplit[scopeSplit.length - 1]]; }