Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

js 赋值问题

clipboard.png

在做一个翻牌游戏,点击第一次时赋值,点击第二次时新赋值,和之前第一次的值,作比较
我把值赋值在全局变量,可是,再次点击还是改变了之前值,没保存之前的值,做不了比较


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

1 Answer

0 votes
by (71.8m points)

保存到全局变量可以理解,不过你为什么第二次翻牌的值要覆盖全局变量?正常的逻辑不应该是这样子的吗?

var prev = null;

function checkCard(card){
   if(prev === null) {
      // 表示单数翻牌,记录牌面
      prev = card;
   } else {
      // 表示偶数翻牌,检查是否相同
      if(prev == card) {
        // 表示两个一样...做你想做的事
      }
      prev = null; // 下一次是单数翻牌
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...