![](/img/trans.png)
[英]How to access the variable from one function to another function in Javascript?
[英]How one function access a variable value from another function in JavaScript?
let count = 0; function increment(){ count += 1; console.log(count); } function decrement(){ count -= 1; console.log(count); } increment(); decrement();
調用increment()
將給出count = 1
,然后調用decrement()
將給出count = 0
。 第二個 function 如何從第一個 function 中獲取 count 的值?
您必須從 function 返回該值並將計數作為參數傳遞給函數:
let count = 0;
function increment(count){
count += 1;
console.log(count);
return count;
}
function decrement(count){
count -= 1;
console.log(count);
return count;
}
var value = increment(count);
value = decrement(count);
value = decrement(increment(count)); //no change of course
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.