简体   繁体   English

更漂亮消除了数学表达式周围的括号

[英]Prettier removes parentheses around mathematical expression

I am trying to slice the array based on the mathematical expressions in React. 我正在尝试基于React中的数学表达式对数组进行切片。 However, prettier extension does not let me do it as it removes parenthesis making the expression incorrect. 但是,更漂亮的扩展名不允许我这样做,因为它删除了括号,使表达式不正确。

Here is example: 这是示例:

const paginateItems = items.slice(
      pageIndex * itemsPerPage,
      (pageIndex * itemsPerPage) * 2
    )

And prettier changes that to 更漂亮的改变是

const paginateItems = items.slice(
      pageIndex * itemsPerPage,
      pageIndex * itemsPerPage * 2
    )

How can I fix this so that it does not force my expression to be incorrect? 我该如何解决这个问题,以免强迫我的表达不正确?

How can I fix this so that it does not force my expression to be incorrect? 我该如何解决这个问题,以免强迫我的表达不正确?

It doesn't make your expression incorrect. 它不会使您的表达不正确。 * is left-associative, meaning that a * b * c is exactly the same as (a * b) * c . *是左关联的,表示a * b * c(a * b) * c完全相同。 The parens in that code don't make any difference to the result. 该代码中的括号对结果没有任何影响。

If your expression were pageIndex * (itemsPerPage * 2) , the parens could make a difference to the result (in programming¹) if itemsPerPage were really big (by causing overflow during itemsPerPage * 2 instead of causing overflow during pageIndex * itemsPerPage , assuming pageIndex is 2 or higher). 如果您的表达式为pageIndex * (itemsPerPage * 2) ,则如果itemsPerPage 确实很大(通过在itemsPerPage * 2期间引起溢出而不是在pageIndex * itemsPerPage期间引起溢出)(假定pageIndexpageIndex * (itemsPerPage * 2) ,则可能会影响结果(在编程中)。 2或更高)。 So I would expect Prettier to leave them alone in that case. 因此,我希望在这种情况下,Prettier不会让他们呆着。 But with (pageIndex * itemsPerPage) * 2 , the () don't have any effect at all. 但是使用(pageIndex * itemsPerPage) * 2()根本没有任何作用。


¹ "in programming" — In the world of mathematics, the parens in pageIndex * (itemsPerPage * 2) wouldn't make any difference either, because multiplication is transitive (I think that's the word): a * b * c is the same as (a * b) * c is the same as a * (b * c) . ¹“在编程中” —在数学世界中, pageIndex * (itemsPerPage * 2)的括号也不会有任何区别,因为乘法是可传递的 (我想这就是这个词): a * b * c相同as (a * b) * ca * (b * c) But in programming we deal with number types that have limited ranges and/or limited precisions at large magnitudes, so the order in which the two multiplication operations happen matters. 但是在编程中,我们处理的数值类型在较大范围内具有有限的范围和/或有限的精度,因此,两个乘法运算发生的顺序很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM