简体   繁体   English

为什么这个 IIFE 返回未定义的错误?

[英]Why is this IIFE returning error undefined?

Why is this returning: Uncaught ReferenceError: winConditions is not defined?为什么会返回:Uncaught ReferenceError: winConditions is not defined?

I'm returning winConditions in the function and then I run the function and then console.log(winConditions), why is it not working?我在 function 中返回 winConditions,然后运行 function,然后运行 console.log(winConditions),为什么它不起作用?

const gameBoard = (() => {
    
    

    const board = [null, null, null, null, null, null, null, null, null]

    let applyWinConditions = () => {  
        const winConditions = [[board[0], board[1], board[2]], [board[3], board[4], board[5]],
        [board[6], board[7], board[8]], [board[0], board[3], board[6]],
        [board[1], board[4], board[7]], [board[2], board[5], board[8]],
        [board[0], board[4], board[8]], [board[2], board[4], board[6]]];
        console.log("e")
        return {
            winConditions
        }
    };

    applyWinConditions();

    console.log(winConditions)

    return {
        applyWinConditions: applyWinConditions,
    }

})();

it seems you are trying to log a variable that is out of scope.看来您正在尝试记录一个超出 scope 的变量。

winConditions is defined inside applyWinConditions, thus not available outside, for console.log to see it. winConditions 是在 applyWinConditions 内部定义的,因此在外部不可用,以便 console.log 可以看到它。

try something like this:尝试这样的事情:

console.log("winConditions", applyWinConditions());

you should see the return of your function, where winConditions has the value you'd expect.您应该看到 function 的返回值,其中 winConditions 具有您期望的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM