如果我調用函數傳遞參數參數(其中調用異步函數B),異步函數B的回調函數C將能夠使用給定的參數參數函數A?如果是的話,如果在函數B start和回調C之間的時間內調用,我會重新調用函數A嗎?在調用函數的回調函數中使用參數
實施例:
function A(param) {
value1 = param;
doc = "hello";
//this is the async function B;
database.insert(doc, function() {
//this is the invoked callback C when the async function is solved.
console.log(value1)
//can i log value1? yes. if yes, will this change if i re-invoke
//function A before the callback is invoked or two different processes will start?
})
}
A('hello');
A('not hello');
想知道這一點,如果在第二時間函數的先前調用的回調函數調用之前,將打印在控制檯右側的值:
你好; 不是你好;
並且從不 不是你好; 不是你好;
導致第二次調用第一次感染。
**用'var'聲明你的局部變量** – Pointy
先謝謝你。這是需要正確的工作還是隻是建議? @Pointy – Morrisda
這只是一個非常重要的習慣,也是一個重要的理解。 – Pointy