Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

javascript colon operator confusion

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

: 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...