为什么我的闭包函数不想在IF语句中重新赋值其父函数变量?

Why doesn#39;t my closure function want to reassign its parent function variable, when within an if statment?(为什么我的闭包函数不想在IF语句中重新赋值其父函数变量?)
本文介绍了为什么我的闭包函数不想在IF语句中重新赋值其父函数变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了用于描述游戏逻辑的工厂函数。我使用内部函数在游戏中切换玩家。问题是,当我试图从内部函数中重新分配当前球员时,它不起作用。当前的玩家永远不会改变。我想这是关于结束的事情,我并不是真的理解。你能给我解释一下我错过了什么吗?下面是我正在编写的一段代码:

const game = (() => {
    let player1 = "Jim";
    let player2 = "Mary";
    let currentPlayer = player1;

    const switchPlayers = () => { //This closure function is supposed to reassign the value of currentPlayer above. But it does not do it.
        if (currentPlayer === player1) {
            currentPlayer = player2;
        } else {
            currentPlayer = player1;
        }
        return currentPlayer;
    };

    return {currentPlayer, switchPlayers};

})();

game.switchPlayers // works as needed and switches the players every time it is invoked but does not reassign the variable within its parent function;
game.currentPlayer // does not get reassigned, when switchPlayers function is invoked, and returns player1 as was assigned at the start;

推荐答案

您的代码返回currentPlayer值,该值在原始对象的属性更新时不会更新。因此,闭包运行得很好,但事实是您的返回值并没有指向更新后的对象,它只是一个新值,在return语句时将保持不变。

您需要使用如下函数返回值。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
let game = (() => {
  let player1 = "Jim";
  let player2 = "Mary";
  let currentPlayer = player1;

  const switchPlayers = () => { //This closure function is supposed to reassign the value of currentPlayer above. But it does not do it.
    if (currentPlayer === player1) {
      currentPlayer = player2;
    } else {
      currentPlayer = player1;
    }
    return currentPlayer;
  };
  
  const getCurrentPlayer = () => currentPlayer;

  return {
    currentPlayer,
    switchPlayers,
    getCurrentPlayer
  };

})();

game.switchPlayers();
console.log(game.currentPlayer);
console.log(game.getCurrentPlayer());

或只需在函数的this指针上定义属性currentPlayer,然后将其用作类型(使用new关键字),如下所示。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
function GameType() {
   let player1 = "Jim";
   let player2 = "Mary";
   this.currentPlayer = player1;

   this.switchPlayers = () => { //This closure function is supposed to reassign the value of currentPlayer above. But it does not do it.
     if (this.currentPlayer === player1) {
       this.currentPlayer = player2;
     } else {
       this.currentPlayer = player1;
     }
     return this.currentPlayer;
   };
 }

let game = new GameType();

console.log(game.currentPlayer);

game.switchPlayers();
console.log(game.currentPlayer);

这篇关于为什么我的闭包函数不想在IF语句中重新赋值其父函数变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

在小程序端input为number类型的表单,调出的键盘只有数字且没有小数点和负号。显然并不符合要求。所以我们用text类型来自己判断 1,必须为数字 2,第一位不是小数点,且只能出现一个小数点 3,负号只能出现在第一位,且只能出现一次 4,如果
本文给大家介绍Javascript js中实现和PHP一样的时间戳格式化函数的方法,具有一定的参考借鉴价值,需要的朋友可以参考下,我们知道在php中有一个date()函数,可以方便的把时间戳格式化为时间字符串。可是在js中,我们要想实现这种效果,要写好
需求是模板字符串中不允许出现script 标签、不允许有javascript: 和 .js 文件引用,主要方法如下: clearScriptTag (str) { const reg = /script[^]*([\S\s]*?)\/script/gim; // 清除标签内 相关 xss 安全代码 const reg1 = /javascript:/gim; const reg2 = / *.js/gim; if (reg.test(str)) { str
javascript中Replace全部替换字符用法实例代码,替换1次和多次,主要是正则表达式 var r= "1\n2\n3\n";//将字母\n替换成分号alert(r.replace("\n",";"));//结果:1;2\n3\n 只替换了第一个var r= "1\n2\n3\n";//将字母\n替换成分号alert(r.replace(/\n/g, ";"));//结果:1;2;3; replac
js输出当前日期和时间的实例代码,具体实例代码如下,有兴趣的朋友可以尝试运行下。 !doctype htmlhtml lang="en" head meta charset="UTF-8" title获取当前时间/title /head body script type="text/javascript" /** *获取当前时间 *format=1精确到天 *format=2精确到秒 */ function
p5.js WebGL 3d graphics covered by 2d background when rotated(P5.js旋转时被2D背景覆盖的WebGL 3D图形)