简体   繁体   English

从函数外部更改值

[英]Change value from outside of the function

I have something like the following: 我有类似以下内容:

function somefunc() {
   function anotherfunc() {
      ...
      if ( m > ... 
      ...
   }
   $(window).on("scroll", anotherfunc);
}

somefunc();

I want to be able to change m value in execution of somefunc("value") (last step in above code snippet - somefunc(); ), so it would transfer the m value to the anotherfunc - but I don't know if I can(able) do so and would like to ask some for your help. 我希望能够在执行somefunc("value")更改m值(上述代码段的最后一步somefunc(); ),因此它将m值传输到anotherfunc但我不知道是否我可以(可以)这样做,并想向您寻求帮助。

 function somefunc(m) { function anotherfunc() { console.log(m) } $(window).on("scroll", function(){ anotherfunc(m); }); } somefunc(1); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Like commented, declare m outside the functions: 像注释一样,在函数外声明m

 var m = 1; console.log('Outside functions: ' + m); function someFunc() { m += 1; console.log('someFunc: ' + m); function otherFunc() { m += 1; console.log('otherFunc: ' + m); } otherFunc(); } someFunc(); 

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

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