本文介绍了javascript冒号运算符混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己正在学习JavaScript.某些javascript令人困惑,

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);

这两行都可以毫无错误地用于json对象中.但是,当我在json对象之外使用这些行时,第二行("orranges":6; )出现错误.这是为什么 ?为什么不给第一行错误( apples:5; ),有什么办法可以在json对象之外使用它?

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 ?

推荐答案

:不是运算符,它构成标签语法的一部分.

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

请参见 MDN

apples是一个标识符.

"orranges"是字符串文字.

您似乎将JSON与对象文字语法混淆了.

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.

这篇关于javascript冒号运算符混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:12