[英]How javascript allows object key type syntax without including ir inside a literal and also variable declaration inside it?
While running these statement in chrome console or inside script tag what I found is that the below syntax which I thought is invalid is working and all the varaibles were exposed globally which I understand could be due to curly braces are interpreted as block but that key: in front is what confuse me.在 chrome 控制台或脚本标签中运行这些语句时,我发现以下我认为无效的语法正在工作,并且所有变量都在全局范围内公开,我理解这可能是由于花括号被解释为块,但该键:前面是什么让我感到困惑。 But I am not sure why and looking for an explaination.
但我不确定为什么并寻找解释。
foo: {
a = 5,
b = 6
}
console.log(a) output ---> 5
console.log(b) output ---> 6
foo: {
a = 5;
b = 6;
}
console.log(a) output ---> 5
console.log(b) output ---> 6
foo: {
a: 5;
b: 6;
}
console.log(a) output ---> 5
console.log(b) output ---> 6
That's because foo: is interpreted as a label and your "ObjectLiteral" get's interpreted as block and the statements inside get executed.那是因为foo:被解释为 label 并且您的“ObjectLiteral” get 被解释为块并且里面的语句被执行。
Your code boils down to the following.您的代码归结为以下内容。
a = 5,
b = 6
Since you don't have any declaration like var
, let
, or const
you're setting a global variable a and b由于您没有像
var
、 let
或const
这样的声明,因此您正在设置全局变量a和b
If you declare a variable by just assigning to it (without using let
, const
, var
or as a function parameter), then it is global.如果您仅通过分配来声明变量(不使用
let
、 const
、 var
或作为 function 参数),那么它是全局的。
The labels do nothing because you have no loop to continue
(or similar) from.标签什么都不做,因为您没有循环可以
continue
(或类似)。
The blocks do nothing because you aren't using let
or const
which would scope variable to blocks.这些块什么都不做,因为您没有使用
let
或const
,这会使 scope 变量变为块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.