gpt4 book ai didi

javascript - java-script 箭头函数返回 (x++,x)

转载 作者:行者123 更新时间:2023-12-01 15:00:33 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What does this symbol mean in JavaScript?

(1 个回答)



What does the comma operator do in JavaScript?

(5 个回答)



When is the comma operator useful?

(15 个回答)


2年前关闭。




我不知道代码如何:const countFrom = x => () => (x++, x);来自 here ,
作品:

const countFrom = x => () => (x++, x);
let a = countFrom(1)

console.log('output:', a()) // output: 2
console.log('output:', a()) // output: 3
console.log('output:', a()) // output: 4
console.log('output:', a()) // output: 5
.as-console-wrapper {min-height: 100%!important; top: 0;}

最佳答案

x是外部 ( x => ) 函数内部的变量,因此所有内部函数 ( () => (x++, x) ) 共享相同的变量。 x++每当内部函数执行时,post 都会增加该变量。逗号运算符 ( ..., x ) 计算为最后一个逗号分隔的表达式 x在这种情况下。

没有逗号运算符可能更容易理解:

 const counter = x => () => x = x + 1;

关于javascript - java-script 箭头函数返回 (x++,x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61431181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com