Discuss / JavaScript / 作业

作业

Topic source

落叶起声

#1 Created at ... [Delete] [Delete and Lock User]

            while (true) {

                yield ++x

            }

snakeman

#2 Created at ... [Delete] [Delete and Lock User]

这个代码虽然测试通过了,实际上是碰运气通过了。代码是有问题的:x使用了全局变量x,每次调用next_id()的时候,x增大了2。

在代码中增加一个输出就看的很清楚。

            while (true) {

                console.log("x: " + x)

                yield ++x

            }

没有啊,就是这样的

2024年1月18日纠正,楼上那位大神说的对,应该要把x先进行块级声明,不然这里就是window的全局变量。


  • 1

Reply