javascript colon operator confusion

烈酒焚心 提交于 2019-12-29 08:42:12

问题


I am learning javascript myself. There is a confusion with some javascript,

price = 14;
name = "Mary";
apples:5; //This line executing without error
"orranges":6; //This line getting error
alert(name);

Those both lines can be used into a json object without any error. But when I am using those lines outside of json object, 2nd line ("orranges":6;) is getting error. Why is that ? And why is not giving error for the first line (apples:5;), is there any way that I can use it outside of json object ?


回答1:


: isn't an operator, it forms part of label syntax.

See MDN

label :
statement

label
Any JavaScript identifier that is not a reserved word.

apples is an identifier.

"orranges" is a string literal.

is there any way that I can use it outside of json object ?

You seem to be confusing JSON with object literal syntax.

You can't use a : as the character that separates a property name from a value in an object when you aren't in the process of defining an object.



来源:https://stackoverflow.com/questions/36022436/javascript-colon-operator-confusion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!