Node.js 中的 <!-- 运算符是什么?
What is the <!-- operator in Node.js?
我在 code golf IRC 房间里看到了下面的 Node.js 片段,这让我很困惑。
for (let x = 100; 1^ <!-- x; x++)
console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - <!-- (3, 5) % (
- "outputs"
* "fizz"
* "buzz"
* "fizzbuzz"
));
代码似乎不正确 -- 当我在 Node 中尝试 运行 时,我得到了一个无限循环。但令我惊讶的是,它确实编译并且 运行!
它似乎使用了几个我以前从未见过的功能。它的^1
的使用让我想起了即将到来的C# range index syntax, and the list almost looks like Markdown, and it seems like some kind of weird currying的模运算符
但是,我特别想了解 <!--
运算符的情况。第一个实例在循环中使用,带有潜在的索引运算符,这看起来像是某种范围迭代语法或其他东西。但我什至猜不到第二个实例中发生了什么。
Node.js 中的 <!--
运算符是什么,应该如何使用?
事情是这样的……你可能认为它是HTML,但这就是语法的来源。
我将该代码准确地放入名为 test.js
的文件中,然后 node test.js
:
100 bottles of beer on the wall.
The beer goes NaN
100 bottles of beer on the wall.
The beer goes NaN
100 bottles of beer on the wall.
The beer goes NaN
--- ad infinitum
这是怎么回事?语法是什么?最后好像多了一个括号!另外,没有{ }
,所以第二行不应该每次迭代都调用!正确的? 错误.
来源如下:
for (let x = 100; 1^ <!-- x; x++)
console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - <!-- (3, 5) % (
- "outputs"
* "fizz"
* "buzz"
* "fizzbuzz"
));
现在,这不完全是 HTML 中需要结束 -->
的 <!--
评论开场白。相反,JavaScript 没有像 /*
那样对待它,而是像 //
那样使用它们:他们评论了 那行 .
的其余部分
for (let x = 100; 1^ // x; x++)
console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - // (3, 5) % (
- "outputs"
* "fizz"
* "buzz"
* "fizzbuzz"
));
嗯..这开始变得更有意义了。让我们杀死评论并调整换行符:
for (let x = 100;
1^ console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - - "outputs" * "fizz" * "buzz" * "fizzbuzz")
);
啊哈!现在我们的无限循环开始变得有意义了。循环的终止条件总是求真值:
1^ console.log(`${x} bottles of beer on the wall.`);
我们递增的 "operator" 是一条带有废话而不是数字的日志消息(即它打印 NaN
的原因):
console.log("The beer goes", x - - "outputs" * "fizz" * "buzz" * "fizzbuzz")
循环体为空:
for ( ...
);
我不确定这段代码的目的是什么,但至少你知道它为什么会按照你观察到的那样做以及为什么 "worked"。
我在 code golf IRC 房间里看到了下面的 Node.js 片段,这让我很困惑。
for (let x = 100; 1^ <!-- x; x++)
console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - <!-- (3, 5) % (
- "outputs"
* "fizz"
* "buzz"
* "fizzbuzz"
));
代码似乎不正确 -- 当我在 Node 中尝试 运行 时,我得到了一个无限循环。但令我惊讶的是,它确实编译并且 运行!
它似乎使用了几个我以前从未见过的功能。它的^1
的使用让我想起了即将到来的C# range index syntax, and the list almost looks like Markdown, and it seems like some kind of weird currying的模运算符
但是,我特别想了解 <!--
运算符的情况。第一个实例在循环中使用,带有潜在的索引运算符,这看起来像是某种范围迭代语法或其他东西。但我什至猜不到第二个实例中发生了什么。
Node.js 中的 <!--
运算符是什么,应该如何使用?
事情是这样的……你可能认为它是HTML,但这就是语法的来源。
我将该代码准确地放入名为 test.js
的文件中,然后 node test.js
:
100 bottles of beer on the wall.
The beer goes NaN
100 bottles of beer on the wall.
The beer goes NaN
100 bottles of beer on the wall.
The beer goes NaN
--- ad infinitum
这是怎么回事?语法是什么?最后好像多了一个括号!另外,没有{ }
,所以第二行不应该每次迭代都调用!正确的? 错误.
来源如下:
for (let x = 100; 1^ <!-- x; x++)
console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - <!-- (3, 5) % (
- "outputs"
* "fizz"
* "buzz"
* "fizzbuzz"
));
现在,这不完全是 HTML 中需要结束 -->
的 <!--
评论开场白。相反,JavaScript 没有像 /*
那样对待它,而是像 //
那样使用它们:他们评论了 那行 .
for (let x = 100; 1^ // x; x++)
console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - // (3, 5) % (
- "outputs"
* "fizz"
* "buzz"
* "fizzbuzz"
));
嗯..这开始变得更有意义了。让我们杀死评论并调整换行符:
for (let x = 100;
1^ console.log(`${x} bottles of beer on the wall.`);
console.log("The beer goes", x - - "outputs" * "fizz" * "buzz" * "fizzbuzz")
);
啊哈!现在我们的无限循环开始变得有意义了。循环的终止条件总是求真值:
1^ console.log(`${x} bottles of beer on the wall.`);
我们递增的 "operator" 是一条带有废话而不是数字的日志消息(即它打印 NaN
的原因):
console.log("The beer goes", x - - "outputs" * "fizz" * "buzz" * "fizzbuzz")
循环体为空:
for ( ...
);
我不确定这段代码的目的是什么,但至少你知道它为什么会按照你观察到的那样做以及为什么 "worked"。