javascript 函数自引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26283868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function Self Reference
提问by Levi Lindsey
In JavaScript, functions are simply objects that can be invoked. So what is the easiest way for the body of a function to reference the actual function object?
在 JavaScript 中,函数只是可以被调用的对象。那么函数体引用实际函数对象的最简单方法是什么?
this
can be used to reference the containing object that a function (or more specifically, a method) is called from. But I believe this
never refers to the actual function object itself.
this
可用于引用从中调用函数(或更具体地说,方法)的包含对象。但我相信this
永远不会指代实际的函数对象本身。
Obviously, bind
, call
, or apply
could be used to change the value of this
for the function. Or bind
could be used to create a version of the function that is always given a reference to itself as its first parameter.
显然,bind
、call
、 或apply
可用于更改this
函数的值。或者bind
可用于创建一个函数版本,该版本始终将自身作为其第一个参数的引用。
But is there any simpler way? I suspect not, but I could be wrong.
但是有没有更简单的方法呢?我怀疑不是,但我可能是错的。
回答by Vivin Paliath
I cannot think of a case where a named function-expression cannot substitute for an anonymous function-expression. So I would suggest naming the function if you are going to call it from within itself (i.e., if you are going to use recursion):
我想不出命名函数表达式不能替代匿名函数表达式的情况。因此,如果您要从其内部调用它(即,如果您要使用递归),我建议命名该函数:
function myFunc(someArg) {
...
...
myFunc(someNewArg);
}
This works even if it is a reference:
即使它是参考,这也有效:
var myFunc = function(someArg) {
...
}
You can even use a recursive IIFE (immediately-invoked function expression) if you don't want to pollute the namespace:
如果您不想污染命名空间,您甚至可以使用递归 IIFE(立即调用的函数表达式):
(function myFunc(arg) {
...
myFunc(someOtherArg);
})(0); //some initial value
Furthermore, doing something like this:
此外,做这样的事情:
someOtherFunction(function myFunc(arg) {
...
myFunc(otherArg);
});
Also works and won't pollute the namespace.
也可以工作并且不会污染命名空间。
回答by JayKuri
This is actually a frustration of mine, because you used to be able to use arguments.callee which would get you a reference to the function. As someone else mentioned, this is no longer possible.
这实际上是我的一个挫折,因为您曾经能够使用 arguments.callee 来获取对函数的引用。正如其他人提到的,这不再可能。
There is a pattern emerging for this... and that is to name the function yourself as well as assign it. It looks like this:
为此出现了一种模式……那就是自己命名函数并分配它。它看起来像这样:
my_obj.myfunc = function foo(arg1, arg2) {
// do stuff
foo.call(this, new_arg1, new_arg2);
}
This way you can reference the named function and go about your business. Note that you do have to use the .call
syntax if you want to call it as a method (so that this
has meaning) but apart from that, it's pretty straightforward.
通过这种方式,您可以引用命名函数并开展您的业务。请注意,.call
如果要将其作为方法调用(这样this
才有意义),则必须使用语法,但除此之外,它非常简单。
Another option is to do a truly functional approach, where you pass the object to be operated on to a function:
另一种选择是采用真正的函数式方法,将要操作的对象传递给函数:
function foo(obj, arg1, arg2) {
// do stuff
// call recursively:
foo(obj, new_arg1, new_arg2);
}
// assign as method:
my_obj.foo = function(arg1, arg2) {
foo(this, arg1, arg2);
}
It could be argued that this is better and more flexible. I think it's a matter of taste, really.
可以说这是更好,更灵活。我认为这是一个品味问题,真的。
回答by Brennan
arguments.callee
inside of a function will be a reference to the function itself, but it's not exactly good form. You should name the function, then you can just use the name inside of the function
arguments.callee
函数内部将是对函数本身的引用,但这不是完全好的形式。您应该命名函数,然后您可以在函数内部使用名称
回答by Rocket Hazmat
I would give your functions a name, then use that to reference them. If you have a declared function or a function variable, then it already has a name.
我会给你的函数一个名字,然后用它来引用它们。如果你有一个声明的函数或函数变量,那么它已经有了一个名字。
function test1(){
// test1();
}
var test2 = function(){
// test2();
};
If you have an anonymous function, you can actually give it a name. That name will onlyexist inside the function.
如果你有一个匿名函数,你实际上可以给它一个名字。该名称仅存在于函数内部。
someFunc(function test3(){
// test3();
});
// test3 will be undefined here
回答by the.reversengineer
You can, but since the function is anonymous, a dummy way to identify it shall be adopted. You can learn a lot on how to do it by simply looking at this beautiful article: Anonymous Recursion in JavaScript
可以,但由于该函数是匿名的,因此应采用一种虚拟方式来识别它。只需查看这篇漂亮的文章:JavaScript 中的匿名递归,您就可以学到很多关于如何做到这一点的知识