我正在尝试执行存储在变量中的函数并将参数传递给它。但是,.apply
在线上,我遇到了错误;Uncaught TypeError: Cannot call method 'apply' of undefined
小提琴:http: //jsfiddle.net/valamas/vzesm/
function target(msg)
{
alert(msg);
}
function mainfunc (func)
{
var args = new Array();
for (var i = 1; i < arguments.length; i++)
args.push(arguments[i]);
console.log('args: ' + args);
//-- different attempts
window[func].apply(this, args);
//window[func].apply(null, Array.prototype.slice.call(arguments, 1));
//this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
$(function ()
{
console.log('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
console.log('ready');
mainfunc('target', 'hello,', 'there!');
});