ES6功能的while循环中的解析赋值不会超出循环? [英] Destructuring assignment in while loop in ES6 function doesn't propogate out of loop?

查看:158
本文介绍了ES6功能的while循环中的解析赋值不会超出循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ES6(通过node-esml)中实现一个简单的GCD算法,并且在一个while循环中更新变量的值,并发现(对我来说)奇怪的行为。这个代码非常有效:

  function gcdWithTemp(x,y){
let [r,rdash] = [x ,y]
while(r!= 0){
q = Math.floor(rdash / r)
temp = r
r = rdash - q * r
rdash = temp
}
return(rdash)
}
console.log(gcdWithTemp(97,34))

返回 1 的预期答案。但是,如果我删除临时变量,而是使用解析赋值来尝试并实现相同的结果:

  function gcdWithDestructuredAssignment(x, y){
let [r,rdash] = [x,y]
while(r!= 0){
q = Math.floor(rdash / r)
[r ,rdash] = [rdash - q * r,r]
}
return(rdash)
}
console.log(gcdWithDestructuredAssignment(97,34))

从未完成,进一步的调试显示r将始终具有分配给第一个值, x 。看来这两个实现应该是一样的?请参阅交换变量



我也尝试使用 var 而不是 let 无效。我是否大大误解了任务的解构或缺少一些微妙的东西?或者是一个错误?

解决方案

这是一个不具有解析赋值的问题,但是使用ASI(自动分号插入)。这两行:

  q = Math.floor(rdash / r)
[r,rdash] = [rdash - q * r,r]

在实践中意味着:

  q = Math.floor(rdash / r)[r,rdash] = [rdash  -  q * r,r] 

这显然不是你的意思。要解决这个问题,请在 [之前添加一个分号:



 

/ div>



当然,您可以在上一行的末尾添加缺少的分号( q = Math.floor(rdash / r ); ),但是由于您一般不使用分号,我假设您使用的是 npm编码风格


I was implementing a simple GCD algorithm in ES6 (through node-esml) and came upon (to me) strange behaviour with updating values of variables inside a while loop. This code works fantastically:

function gcdWithTemp(x, y) {
  let [r, rdash] = [x, y]
  while (r != 0) {
    q = Math.floor(rdash / r)
    temp = r
    r = rdash - q * r
    rdash = temp
  }
  return(rdash)
}
console.log(gcdWithTemp(97, 34))

Returning the expected answer of 1. However, if I remove the temporary variable and instead use destructuring assignment to try and achieve the same results:

function gcdWithDestructuredAssignment(x, y) {
  let [r, rdash] = [x, y]
  while (r != 0) {
    q = Math.floor(rdash / r)
    [r, rdash] = [rdash - q * r, r]
  }
  return(rdash)
}
console.log(gcdWithDestructuredAssignment(97, 34))

It never completes, further debugging shows that r will always have the first value assigned to, x. It seems that these two implementations should be identical? see Swapping variables

I've also tried with using var instead of let to no avail. Am I drastically misunderstanding the point of destructuring assignment or missing something subtle? Or is it a bug?

解决方案

That's a problem not with destructuring assignment, but with ASI (automatic semicolon insertion). These two lines:

q = Math.floor(rdash / r)
[r, rdash] = [rdash - q * r, r]

in practice mean this:

q = Math.floor(rdash / r)[r, rdash] = [rdash - q * r, r]

which obviously is not what you meant. To fix that, add a semicolon in front of [:

function gcdWithDestructuredAssignment(x, y) {
  let [r, rdash] = [x, y]
  while (r != 0) {
    q = Math.floor(rdash / r)
    ;[r, rdash] = [rdash - q * r, r]
  }
  return(rdash)
}
console.log(gcdWithDestructuredAssignment(97, 34))

Of course you can add the missing semicolon at the end of the previous line instead (q = Math.floor(rdash / r);), but since you generally don't use semicolons, I assumed that you're using npm coding style.

这篇关于ES6功能的while循环中的解析赋值不会超出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆