Jquery回调函数和变量作用域

huangapple go评论53阅读模式
英文:

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

是的,ac1[[Scope]] 中,而 bc2[[Scope]] 中。

英文:

yes a in [[Scope]] of c1 and b in [[Scope]] of c2

huangapple
  • 本文由 发表于 2023年6月25日 21:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定