英文:
Jquery callback function and variable scope
问题
以下是翻译好的部分:
这段代码在预期的情况下能够正常工作,但我不确定变量作用域是如何处理的。
英文:
Is the below Jquery code a valid one? Specifically, when callback functions are called from fun2(), will the variables a and b be in scope?
Some details on how callback functions and variables are handled in this scenario will be helpful.
function fun1(a, b)
{
var c1 = function () {
console.log(a);
}
var c2 = function () {
console.log(b);
}
fun2(c1, c2);
}
function fun2(callback1, callback2)
{
//Display a modal and invoke callback functions based on button click
$('#btnOk').click(function(){
callback1();
});
$('#btnCancel').click(function(){
callback2();
});
}
This code works as expected, but I am not sure how the variable scopes are handled.
答案1
得分: 0
是的,a 在 c1 的 [[Scope]] 中,而 b 在 c2 的 [[Scope]] 中。
英文:
yes a in [[Scope]] of c1 and b in [[Scope]] of c2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论