简体   繁体   English

可能将javascript变量的作用域限定为一个函数,并且该函数对其自身进行递归调用?

[英]Possible to scope a javascript variable to a function and that function's recursive calls to itself?

I am in a situation where I have a loop that is calling a function. 我处于一种正在调用函数的循环的情况下。 The function will make recursive calls to itself once called. 一旦被调用,该函数将对其进行递归调用。

Is there a way that I can scope a variable to the function and the chain of recursive calls generated from the first call? 有没有一种方法可以使变量作用于函数以及从第一个调用生成的递归调用链?

Something like this: 像这样:

for(var i=0;i<100;i++)
{
  myFunction();
}

function myFunction()
{
   var someNumber = 200;
   someNumber -= 10;
   if( someNumber > 0)
   {
      myFunction();
   }
}

where at the second iteration of the first call to someNumber would be 190 and not 200. Is this possible to accomplish? 在第一次调用someNumber的第二次迭代中,它将是190,而不是200。这有可能实现吗?

If there is anything confusing here, please let me know. 如果这里有什么令人困惑的地方,请告诉我。

while(var i=0;i<100;i++)
{
  myFunction(200);
}

function myFunction(someNumber)
{
   someNumber -= 10;
   if( someNumber > 0)
   {
      myFunction(someNumber);
   }
}

Yes, use an inner function to do the recursion: 是的,使用内部函数进行递归:

for (var i = 0; i < 100; i++)
{
    myFunction();
}

function myFunction()
{
    var someNumber = 200;
    (function innerFunction()
    {
        someNumber -= 10;
        if( someNumber > 0)
        {
            innerFunction();
        }
    })();
};

Note, you have a syntax error. 注意,您有语法错误。 Your while needs to be for . while必须for

Edit: Perhaps your real code is different and calls for a recursive function, but your example would be much simpler by just using a loop: 编辑:也许您的实际代码有所不同,并且需要递归函数,但是仅使用循环,您的示例就会简单得多:

function myFunction()
{
    for (var someNumber = 200; someNumber > 0; someNumber -= 10)
    {
    }
};

Yes and no--you should be able to accomplish the task by passing the value of the variable in as a parameter of myFunction . 是和否-您应该能够通过将变量的值作为myFunction的参数传入来完成任务。 The first call will need to set the starting value, then pass it along for future invocations to modify. 第一次调用将需要设置起始值,然后将其传递给以后的调用进行修改。

while(var i=0;i<100;i++)
{
  myFunction();
}


function myFunction(seed) {
    if (seed == undefined)
    {
        seed = 200;
    }

    alert(seed);

    var newSeed = seed - 50;
    if (seed > 0)
    {
        myFunction(newSeed);
    }
};

I think using objects would be the way to go if you want to do something where you are actually scoping variables. 我认为如果您想在实际确定变量的范围内进行操作,则使用对象将是一种方法。

function myObject(_someNumber) {
    var someNumber = _someNumber;

    this.myFunction = function() {
        this.someNumber -= 10;
        if(this.someNumber > 0)
        {
           this.myFunction(this.someNumber);
        }
    }
}

for(var i=0;i<100;i++)
{
  new myObject().myFunction(someNumber);
}

You want a closure. 您想要关闭。 Well, you might not want it, but it will do what you are asking. 好吧,您可能不想要它,但是它将满足您的要求。

(function() {
    var someNumber=200;
    myFunction=function() {
        someNumber-=10;
        if (someNumber > 0) {
            myFunction();
        }
    }
})();

But properly passing parameters is probably a better idea. 但是正确传递参数可能是一个更好的主意。

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

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