简体   繁体   English

javascript 如何允许 object 键类型语法不包括 ir 在文字中和变量声明中?

[英]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由于您没有像varletconst这样的声明,因此您正在设置全局变量ab

If you declare a variable by just assigning to it (without using let , const , var or as a function parameter), then it is global.如果您仅通过分配来声明变量(不使用letconstvar或作为 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.这些块什么都不做,因为您没有使用letconst ,这会使 scope 变量变为块。

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

相关问题 如何在不使用javascript函数的情况下使用对象文字的属性 - How to use properties of an object literal without being inside a function in javascript JavaScript:在对象声明中声明变量 - JavaScript: Declare variable inside object declaration 如何在 JavaScript object 文字中使用变量作为键? - How to use a variable for a key in a JavaScript object literal? 是否可以将变量的值用作对象声明中的对象键? - Is it possible to use a variable's value as an object key inside object declaration? 如何将 RegExp 放入带有 javascript 变量的 JavaScript 对象字面量(又名对象初始值设定项)中 - How to put a RegExp inside a JavaScript object literal aka object initializer with a javascript variable 了解javascript对象文字内的键名的方法 - Ways to get to know of key names inside a javascript object literal 在 Javascript 的正则表达式中包含变量 - Including variable inside regex in Javascript 如何在没有“this”的情况下调用object内部的object密钥 - How to call key of object inside of object without "this" 如何根据对象内的值定义文字类型 - how to define a literal type based on a value inside an object Javascript-如何在ajax调用中将“ this”绑定到对象文字 - Javascript - how to bind 'this' inside an ajax call to the object literal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM