JavaScript函数重新定义 [英] JavaScript function redefinition
本文介绍了JavaScript函数重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以从自己的体内重新定义JavaScript函数。例如,我可以执行以下操作吗?
Is it possible to redefine a JavaScript function from within its own body. For example, could I do the following?
function never_called_again(args) {
// Do some stuff
never_called_again = function (new_args) {
// Do some new stuff
}
}
以上是否有效,是否具有正确的语义?我不想用旧的函数名创建一个新的全局变量,因为我试图在全局范围内做这种事情,但是从各种对象范围开始,我不希望当我发生名字冲突时在这些本地范围内重新定义函数。
Is the above valid and does it have the correct semantics? I don't want to create a new global variable with the old function name, because I'm trying to do this kind of thing not in the global scope, but from various object scopes, and I don't want name clashes when I redefine the function within those local scopes.
推荐答案
是的,您可以通过这种方式重新定义函数。
Yes, you can redefine a function that way.
运行此:
function never_called_again(args) {
console.log('Func 1', args);
never_called_again = function (new_args, x) {
console.log('Func 2', new_args, x);
}
}
never_called_again('arg A', 'arg B');
never_called_again('arg A', 'arg B');
收益率:
Yields this:
Func 1 arg A
Func 2 arg A arg B
这篇关于JavaScript函数重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文