简体   繁体   English

在不使用名称/ arguments.callee的情况下引用当前函数

[英]referring to the current function without using its name/arguments.callee

I have done a fair amount of research but have not been able to find any answers, to what seems like a simple question: 我做了大量的研究,但未能找到任何答案,看似简单的问题:

I want to associate a property with a function (to use as a static variable in this function), like so: 我想将属性与函数关联(在此函数中用作静态变量),如下所示:

function foo() {
    if (!foo.counter) {
        foo.counter = 1;
    }
    else {
        foo.counter++
    }
    // rest of the function code goes here...
}

If I change the name of the function later, I don't want to have to change references to it inside function definition. 如果我稍后更改函数的名称,我不想在函数定义中更改对它的引用。 So, is there a way to refer to the currently executing function? 那么,有没有办法引用当前正在执行的函数? (other than arguments.callee which is now deprecated). (除了现在已弃用的arguments.callee )。 A keyword like thisFunction ? thisFunction这样的关键字?

If not, what is the reason for not having something like this? 如果没有,没有这样的东西是什么原因?

I do not know of an identifier or keyword like thisFunction (besides arguments.callee ) that JavaScript exposes, but one way to achieve the same effect is to name the function foo in a closure, and return foo from that closure. 我不知道JavaScript公开的标识符或关键字如thisFunction (除了arguments.callee ),但实现相同效果的一种方法是在闭包中命名函数foo ,并从该闭包返回foo That way you can always use the name foo within foo to refer to itself, regardless of what variable it's assigned to by your program: 这样,你可以随时使用的名称foofoo指本身,无论什么变量它是由你的程序分配的:

var fn = (function() {
  function foo() {

     if (!foo.counter) {
        foo.counter = 1;
    }
    else {
        foo.counter++
    }
    // rest of the function code goes here...
  }

  return foo;
})();
function foo() {
  if (typeof foo.counter=="undefined") {
    foo.counter = 1;
  }
  else {
    foo.counter++
  }
  return foo;
}

var x = new foo();
alert(x.counter);  //1
var y = new foo();
alert(x.counter); //2

If you don't need to refer to the counter outside of the function, this will work. 如果您不需要在功能之外引用计数器,这将起作用。 Instead of setting a variable of the function itself, you define a variable outside of the function and let the function modify that instead. 您可以在函数外部定义一个变量,而不是设置函数本身的变量,而是让函数修改它。 Every time you call makeFoo , it makes a new variable x and returns a function using x . 每次调用makeFoo ,它都会生成一个新变量x并使用x返回一个函数。 Every time you call the returned function, it still refers to the same x created by makeFoo . 每次调用返回的函数时,它仍然引用makeFoo创建的相同x Additionally, this entirely encapsulates the variable, ensuring that almost nothing outside of the returned function can change it (it definitely won't be changed by most things outside of the returned function, but there is enough funky javascript reflection that I can't guarantee that nothing will change it). 另外,这完全封装了变量,确保返回函数之外几乎没有任何东西可以改变它(它肯定不会被返回函数之外的大多数东西改变,但是有足够的时髦javascript反射,我不能保证什么都不会改变它)。 Certainly, moving the function between variables won't affect the counter (ie x=makeFoo();y=x;x=null; ). 当然,在变量之间移动函数不会影响计数器(即x=makeFoo();y=x;x=null; )。

function makeFoo() {
  var x=0;
  return function () {
    x++;
    return x;
  }
}

//different counters
x=makeFoo();
y=makeFoo();
x();//returns 1
y();//returns 1
x();//returns 2
y();//returns 2

//same counter
x=makeFoo();
y=x;
x();//returns 1
y();//returns 2
x();//returns 3
y();//returns 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM