什么是var = var&& " * QUOT;用Javascript表示 [英] What does var = var && "*" mean in Javascript
问题描述
我正在尝试通过阅读它来研究一些被讨论的代码,并且我做得很好,直到遇到这个:
I am trying to work out some obfusicated code by reading it, and I was doing pretty well until a came across this:
a = a && "*"
现在我还是Javascript的新手,这些缩短的不常见的javascript代码仍然非常陌生对我来说,这是我第一次碰到它们。
Now I am still quite new to Javascript and these shortened uncommon javascript codes are still very foreign to me, this is the first time I have come across them.
有人知道这是做什么的吗?我在一个javascript代码测试器广告中尝试过它只返回*,所以我不知道。
Does anybody know what this does? I attempted it in a javascript code tester ad it just returned *, so I do not know.
另外,如果有人知道我在哪里可以找到这些不寻常的东西代码行,这将是非常有帮助的。它们都缩短了,并且有各种类似的东西。
Also, if anybody knows where I can look to find out what these uncommon lines of code do, that would be very helpful. They are all shortened and are sorts of things like this and
a = a || b
(我知道那个人做了什么)
(I know what that one does)
但是如果这种javascript或我可以看到的引用有某种名称,那将非常有用,我已经在谷歌搜索了几个小时。
But If there is some sort of name for this kind of javascript or a reference I can look at, that would be very helpful, I have been scouring Google for hours.
谢谢
推荐答案
如果 a
是真的,它会分配*
到 a
。
If a
is truthy, it assigns "*"
to a
.
如果 a
是假的,它保持不变。
If a
is falsy, it remains untouched.
&&
具有短路语义:A复合表达式(e1)&& (e2)
—其中 e1
和 e2
是任意表达式—评估为
&&
has short-circuit semantics: A compound expression (e1) && (e2)
—where e1
and e2
are arbitrary expressions themselves—evaluates to either
-
e1
如果e1
在布尔上下文中评估为false—e2
不 已评估 -
e2
如果e1
在布尔上下文中评估为true
e1
ife1
evaluates to false in a boolean context—e2
is not evaluatede2
ife1
evaluates to true in a boolean context
不 暗示 e1
或 e2
和整个表达式(e1)&& (e2)
需要评估为true或false!
This does not imply that e1
or e2
and the entire expression (e1) && (e2)
need evaluate to true or false!
在布尔上下文中,以下值计算为 false
根据规范 :
In a boolean context, the following values evaluate to false
as per the spec:
- null
- undefined
- ± 0
- NaN
- false
- 空字符串
- null
- undefined
- ±0
- NaN
- false
- the empty string
所有 1 其他值被视为 true
。
上述值简洁地称为falsy,其他值为truthy。
The above values are succinctly called "falsy" and the others "truthy".
应用于您的示例: a = a&& *
根据上述&&& $ c的短路评估规则$ c>,表达式的计算结果为
a
如果 a
是 falsy ,那么反过来分配给 a
,即语句简化为 a = a
。
According to the aforementioned rules of short-circuit evaluation for &&
, the expression evaluates to a
if a
is falsy, which is then in turn assigned to a
, i.e. the statement simplifies to a = a
.
如果
是 truthy ,则右侧的表达式计算为 *
,又被分配到 a
。
If a
is truthy, however, the expression on the right-hand side evaluates to *
, which is in turn assigned to a
.
至于你的第二个问题:(e1)|| (e2)
具有类似的语义:
整个表达式的计算结果为:
The entire expression evaluates to:
-
e1
如果e1
是真实的 -
e2
如果e1
是假的
e1
ife1
is truthye2
ife1
is falsy
1 例外
这篇关于什么是var = var&& " * QUOT;用Javascript表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!