首页 > 解决方案 > 访问循环时,在循环外声明的变量确实会发生变化 - 节点

问题描述

现在,当 if 明显进入循环并且应该更改为 false 时,我的函数将返回 true,如下所示:

private recalculateGridViewBottomToTop = (window: fin.OpenFinWindow): boolean => {
const toolbarWindow = fin.desktop.Window.getCurrent();
if (this.windows != null) {
  let shiftFromRightToLeft = true;
  this.windows.forEach((win) => {
    window.getBounds((currentBounds) => {
      win.getBounds((bounds) => {
        if (bounds.top >= currentBounds.bottom! && bounds.left === currentBounds.left) {
          win.leaveGroup();
          win.setBounds(bounds.left, bounds.top - currentBounds.height, bounds.width, bounds.height);
          toolbarWindow.mergeGroups(win);
          shiftFromRightToLeft = false;
        }
      });
    });
  });
  return shiftFromRightToLeft;
}

shiftFromRightToLeft 应该已经从true变为false我想知道为什么没有发生这种情况?

标签: node.jstypescript

解决方案


根据您的代码,如果条件为真,if (bounds.top >= currentBounds.bottom! && bounds.left === currentBounds.left) {则 shiftFromRightToLeft 将更改false,否则将保持不变true

看看下面的例子:

if(true){
let a = true
  if(false){
    a = false
  }
    console.log(a)  // output :  true
}

推荐阅读