请在javascript中解释冒号的这种用法 [英] Please explain this usage of a colon in javascript

查看:114
本文介绍了请在javascript中解释冒号的这种用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个库,我经常检查Closure Compiler输出的结果,看看它是如何做的(我确实有单元测试,但我仍然希望看到编译后的代码,以了解它如何更好地压缩)。

I'm making a library, and I often inspect the result of Closure Compiler's output to see how it's doing things (I do have unit tests, but I still like to see the compiled code for hints of how it could compress better).

所以,我发现了这段非常奇怪的代码,这是我以前从未见过的。

So, I found this very weird piece of code, which I never seen before.

variable : {
    some();
    code()
}

注意:这是不是对象文字!此外,在任何地方都没有 a ?:有条件的

该代码位于常规功能块中 IIFE )。

Note: this is not an object literal! Also, there is no ? anywhere that would make it a ?: conditional.
That code is in a regular function block (an IIFE).

变量,in这种情况下,是一个未定义的变量。 没有代码使它成为真,假,或其他,只是为了确保,我在那里放了一个 console.log ,的确,我得到 ReferenceError

variable, in this case, is an undefined variable. There's no code making it true, false, or whatever, and just to make sure, I put a console.log in there and indeed, I get a ReferenceError.

请注意我在IE8中测试我的代码,所以这不仅仅是在现代浏览器中。它似乎是标准的,简单的旧javascript。

Please do note that I test my code in IE8 too, so this isn't just in modern browsers. It seems to be standard, plain old javascript.

所以让我们试一试。点击Chrome的控制台,我明白了:

So let's experiment with it. Firing up Chrome's console, I get this:

undeclaredVariable:{console.log('does this get logged?')} // yes it does.
trueValue:{console.log('what about this?')}               // same thing.
falseValue:{console.log('and this?')}                     // same thing.

但是......

(true):{console.log('does this work too?')} // SyntaxError: Unexpected token :

...和......

...and...

so?{console.log('is this a conditional?')}:{alert(123)} // Unexpected token .

那该怎么办?

thisThing:{console.log('is used to declare a variable?')}
thisThing // ReferenceError: thisThing is not defined

如果有人可以向我解释这段代码的意图,或者至少是它的作用,我会很高兴。

Please, I'd love it if someone could explain to me what this code is meant to do, or at least what it does.

推荐答案

这是标签


提供一个带有标识符的语句,您可以使用
中断来引用该标识符或者继续声明。

Provides a statement with an identifier that you can refer to using a break or continue statement.

例如,您可以使用标签来标识循环,然后使用
break或continue语句来指示程序是否应该
中断循环或继续执行。

For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

你看到的另一个常见的地方是当人们坚持精彩而无用的时候code>的javascript: 关于事件处理程序。

Another common place you see it is when people stick the wonderful and useless javascript: on event handlers.

这篇关于请在javascript中解释冒号的这种用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆