简体   繁体   English

是什么

[英]What is the <!--​ operator in Node.js?

I came across the following Node.js snippet in a code golf IRC room, and it is confusing me. 我在代码高尔夫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"
    ));

The code doesn't seem to be correct -- when I try running it in Node, I get an infinite loop. 代码似乎不正确 - 当我尝试在Node中运行它时,我得到一个无限循环。 But to my surprise, it does compile and run! 但令我惊讶的是,它确实编译并运行!

It seems to be using several features I haven't seen before. 它似乎使用了我以前从未见过的几个功能。 The use of ^1 of it reminds me of the upcoming C# range index syntax , and the list almost looks like Markdown , and it seems like some kind of weird currying of the modulo operator is going on. ^1的使用让我想起即将推出的C#范围索引语法 ,这个列表几乎看起来像Markdown ,似乎模数运算符的某种奇怪的currying正在进行中。

However, I'd particularly like to understand what's going on with the <!-- operator. 但是,我特别想了解<!--运算符发生了什么。 The first instance is being used in a loop, with the potential index operator, which seems like some kind of range iteration syntax or something. 第一个实例正在循环中使用,带有潜在的索引运算符,这似乎是某种范围迭代语法或其他东西。 But I can't even guess what's happening in the second instance. 但我甚至猜不出第二次发生了什么。

What is the <!-- operator in Node.js and how should it be used? Node.js中的<!--运算符是什么以及它应该如何使用?

Here's the thing... you may not think that it's HTML, but that's where the syntax comes from. 这就是......你可能不认为它是HTML,但这就是语法的来源。

I put exactly that code into a file named test.js , then node test.js : 我将该代码完全放入名为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

So what's going on here? 那么这里发生了什么? And what's with the syntax? 什么是语法? It looks like there's an extra parenthesis at the end! 看起来最后有一个额外的括号! Also, there is no { } , so the second line shouldn't be called each iteration! 此外,没有{ } ,因此每次迭代都不应调用第二行! Right? 对? Wrong . 错了

Here's what the source looks like: 以下是源代码:

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"
    ));

Now, this isn't quite the <!-- comment opener in HTML that would require a closing --> . 现在,这不是HTML中的<!-- comment opener,需要关闭--> Instead, of treating it like /* , JavaScript uses these like // : they comment the remainder of that line . 相反,将它视为/* ,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"
    ));

Hm.. This is starting to make a bit more sense. 嗯..这开始变得更有意义了。 Let's kill the comments and adjust the newlines: 让我们删除评论并调整换行符:

for (let x = 100;
     1^ console.log(`${x} bottles of beer on the wall.`);
     console.log("The beer goes", x - - "outputs" * "fizz" * "buzz" * "fizzbuzz")
    );

AHA! AHA! Now our infinite loop is starting to make sense. 现在我们的无限循环开始变得有意义了。 The termination condition of the loop always evaluates truthy: 循环的终止条件总是评估真实性:

1^ console.log(`${x} bottles of beer on the wall.`);

Our incrementing "operator" is a log message with nonsense instead of a number (ie the reason it prints NaN ): 我们递增的“运算符”是一个带有废话而不是数字的日志消息(即它打印NaN的原因):

console.log("The beer goes", x - - "outputs" * "fizz" * "buzz" * "fizzbuzz")

And the loop body is empty: 循环体是空的:

for ( ...
    );

I'm not sure what this code was intended to do, but at least you know why it was doing exactly what you observed it to do and why it "worked". 我不确定这段代码的目的是什么,但至少你知道为什么它正在完成你所观察到的以及为什么它“工作”。

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

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