Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - Uncaught TypeError: modal.openModal is not a function

I am trying to trigger a modal popup from within an IIFE game controller module in JavaScript. The modal popup is situated in its own module and is returning the required function.

I have only recently learned about modules and so it is definitely plausible that I am missing something obvious, but after hours of staring at my code and attempting to scour the web, I am struggling to find the solution.

Below is the game controller module:

// Game Controller
const gameController = (() => {

    // Create players
    let player1 = PlayerFactory("Player 1", "images/player.png");
    let player2 = PlayerFactory("Player 2", "images/computer.png");

    // Initialise game variables
    let round = 0;
    let maxRounds = 8;
    let turn = 0;
    let player1Score = 0;
    let player2Score = 0;
    

    // Main game function
    const play = function (e) {
      if (round == maxRounds) {
        // Increment both players' scores
        player1Score++;
        player2Score++;
        // Display modal pop-up for tie
      }
      switch (turn) {
        case 0:
          if (player1.play(e)) {
            e.target.dataset.player = 0;
            round++;
            turn = 1;
            if (checkWinner() == 0) {
              // Increment player 1 score
              player1Score++;
              // Display modal popup for player 1 win
              modal.openModal();
            }
          }
          break;
        case 1:
          if (player2.play(e)) {
            e.target.dataset.player = 1;
            round++;
            turn = 0;
            if (checkWinner() == 0) {
              // Increment player 2 score
              player2Score++;
              // Display modal popup for player 2 win
            }
          }
          break;
          }
      }

    // Check winner function
    const checkWinner = function () {

      // Initialise winner variable
      let gameWinner = '';

      // For loop checking for horizontal wins
      for (i=0; i<9; i=i+3) {
        if (gameBoard.boardArray[i].dataset.player) {
          if ((gameBoard.boardArray[i].dataset.player == gameBoard.boardArray[i+1].dataset.player) && (gameBoard.boardArray[i+1].dataset.player == gameBoard.boardArray[i+2].dataset.player)) {
            gameWinner = gameBoard.boardArray[i].dataset.player;
            return gameWinner;
          }
        }
      }

      // For loop checking for vertical wins
      for (i=0; i<3; i++) {
        if (gameBoard.boardArray[i].dataset.player) {
          if ((gameBoard.boardArray[i].dataset.player == gameBoard.boardArray[i+3].dataset.player) && (gameBoard.boardArray[i+3].dataset.player == gameBoard.boardArray[i+6].dataset.player)) {
            gameWinner = gameBoard.boardArray[i].dataset.player;
            return gameWinner;
          }
        }
      }

      // For loop checking for diagonal wins
      if (gameBoard.boardArray[0].dataset.player) {
        if ((gameBoard.boardArray[0].dataset.player == gameBoard.boardArray[4].dataset.player) && (gameBoard.boardArray[4].dataset.player == gameBoard.boardArray[8].dataset.player)) {
          gameWinner = gameBoard.boardArray[0].dataset.player;
          return gameWinner;
        }
      }

      // For loop checking for diagonal wins
      if (gameBoard.boardArray[2].dataset.player) {
        if ((gameBoard.boardArray[2].dataset.player == gameBoard.boardArray[4].dataset.player) && (gameBoard.boardArray[4].dataset.player == gameBoard.boardArray[6].dataset.player)) {
          gameWinner = gameBoard.boardArray[2].dataset.player;
          return gameWinner;
        }
      }
    }

    // Click event listener to call play function
    window.addEventListener('DOMContentLoaded', (event) => {
      let cells = document.querySelectorAll(".game-cell").forEach(cell => {
        cell.addEventListener("click", play);
      })
    });
    
    return {

    };
})();

And here is the modal popup module:

// Modal Popup
const modal = (() => {

  const modalPopup = document.querySelector(".modal");

  const openModal = function () {
    modalPopup.style.display = "block";
  }

  return {
    openModal
  }
})

Currently, when I trigger the modal popup with the following line (in the game controller):

              // Display modal popup for player 1 win
              modal.openModal();

I receive this error in the console:

Uncaught TypeError: modal.openModal is not a function

If anybody could provide some assitance here then it would be highly appreciated.

Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is beacuse modal is a function, call it instead with parentheses, like this

modal().openModal()

It is simple to check if is or not defined

if (modal.openModal)
    alert("is modal");
else
    alert("isnt modal");

This will alert "isnt modal" because modal is a funcion

if (modal().openModal)
    alert("is modal");
else
    alert("isnt modal");

This will alert "is modal", this is correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...