[英]Change value from outside of the function
我有類似以下內容:
function somefunc() {
function anotherfunc() {
...
if ( m > ...
...
}
$(window).on("scroll", anotherfunc);
}
somefunc();
我希望能夠在執行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>
像注釋一樣,在函數外聲明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.