修复对象内多个js嵌套函数的作用域

Fixing scope in multiple js nested functions within object

本文关键字:嵌套 函数 作用域 js 对象      更新时间:2023-09-26
var game = {
    num: 0,
    playerOne: undefined,
    playerTwo: undefined,
    setPlayerNum: function() {
        swal({ 
                title: "Players",
                text: "Enter Number of Players:",
                type: "input",
                showCancelButton: true,
                closeOnConfirm: false,
                animation: "slide-from-top",
            },
            function (inputValue) {
                //Figure out code to put here
                if (inputValue === "") {
                    swal.showInputError ("Please a Number");
                    return false;
                }
                if (inputValue == 1){
                    swal("Nice!", "You Entered: " + inputValue, "success");
                } else if (inputValue == 2) {
                    swal("Twice as Nice!", "You Entered: " + inputValue, "success");
                }
         });
    }
};

我有一堆嵌套函数,我对作用域的把握已经离开了我。我怎样才能使它使函数(inputValue)存储从setPlayerNum: function()在playerOne:未定义的输入?谢谢你的帮助!

您需要从setPlayerNum函数返回一些东西。