我正在尝试通过另一个引用变量将新函数分配给 javascript 中的变量。我有以下代码:
grp0 =
{
"grp0_0":
{
"grp0_0_0": function()
{
console.log('test', 0, 0, 0);
},
"grp0_0_1": function()
{
console.log('test', 0, 0, 0);
}
},
"grp0_1":
{
"grp0_1_0": function()
{
console.log('test', 0, 1, 0);
},
"grp0_1_1":
{
"grp0_1_1_0": function()
{
console.log('test', 0, 1, 1, 0);
},
"grp0_1_1_1": function()
{
console.log('test', 0, 1, 1, 1);
}
}
},
"grp0_2": function()
{
console.log('test', 0, 2);
}
}
function replaceFunc(group, names)
{
if (typeof group === 'function')
{
console.log('replace function:', names);
group = function()
{
console.log('something else');
}
}
else
{
$.each(group, function(name, value)
{
replaceFunc(value, names.concat(name));
});
}
}
grp0.grp0_1.grp0_1_0();
replaceFunc(grp0, []);
grp0.grp0_1.grp0_1_0();
正如预期的那样,这输出:
test 0 1 0
replace function: ["grp0_0", "grp0_0_0"]
replace function: ["grp0_0", "grp0_0_1"]
replace function: ["grp0_1", "grp0_1_0"]
replace function: ["grp0_1", "grp0_1_1", "grp0_1_1_0"]
replace function: ["grp0_1", "grp0_1_1", "grp0_1_1_1"]
replace function: ["grp0_2"]
test 0 1 0
但我希望最后一行是:
something else
如何更改原始功能?
感谢您的任何帮助。