This is big Ice’s 12th original article in 2021, so work with Big Ice on the front end!! 💪


Writing in the front

Two days ago, I saw a colleague’s code, he would add “” to the key value when writing an object, so I reviewed the knowledge about whether to add “” to the key value of an object.

What difference does it make? First, a review of the identifier principle:

1. The first letter must be a letter, underscore (_), or dollar sign ($), and cannot be a number.

2, except the first letter, other characters can be letters, digits, underscores, or dollar signs ($).

3. Ordinary identifiers (used as variable names, function names, and jump tags in loops) cannot be reserved characters or keywords.

In strict mode, arguments and eval cannot be used as variable, function, or parameter names.

So we’re going to talk about this in terms of conforming identifiers and non-conforming identifiers.

Match the identifier

Obj [“key”] and obj.key can be read.

The code is as follows:

let obj1 = {
    name: "Xiao Ming".$age: 18._like: "A wide range of hobbies."
}
let obj2 = {
    "name": "Little red"."$age": 16."_like": "Few hobbies."
}

console.log(obj1) 
// {name: "xiao Ming ", age: 18, _like:" wide "}
console.log(obj1.name, obj1.$age, obj1._like) 
// Xiaoming has many hobbies
console.log(obj1['name'], obj1['$age'], obj1['_like']) 
// Xiaoming has many hobbies

console.log(obj2) 
// {name: "", $age: 16, _like:" "}
console.log(obj2.name, obj2.$age, obj2._like) 
// Xiao Hong 16 has few hobbies
console.log(obj2['name'], obj2['$age'], obj2['_like']) 
// Xiao Hong 16 has few hobbies
Copy the code

Not conforming to the identifier

“” must be added to the key, and the value corresponding to the read key can only be obj[“key”].

The code is as follows:

let obj = {
    "0": "Xiao Ming"."0name": "Little Ming Ming".// Unexpected token: Invalid or unexpected token
    "": 18."RMB": "Ha ha"."": "A wide range of hobbies."
}
console.log(obj) / / {0: "xiao Ming," 0 name: "little Ming", "" : 18, RMB:" ha ha ", "" :" the hobby is widespread "}
console.log(obj. 0, obj.0name) / / an error
console.log(obj['0'], obj['0name'], obj[' '], obj[A '$'], obj[' ']) // Xiao Ming xiao Ming 18 ha ha ha wide hobbies
Copy the code

Obj [“key”] = obj[“key”] = obj[“key”]

For the study of Symbol types, please refer to my article: “Read it, understand the series”. Promise me, start using Symbol after reading it, ok?


If you don’t want more, check out Big Ice’s “Read It And Understand it series”, which aims to present common concepts or methods in an easy-to-understand way. Welcome to click here to discuss and learn:

  • CSS3’s calc() function
  • “Read to understand the series” talk about the principle and implementation of data buried point
  • Does Ajax single-handedly create the entire front-end ecosystem?
  • Is it complicated to manage the rights in the project of “Read the Series”?
  • “You know it when You’re Done” Oh, my God! Throttling and shaking are so simple to understand
  • 15 ways to play with strings
  • Substr (), slice(), substring(

Original is not easy, if there are mistakes, welcome to correct.

If it helps you, please give big Ice quietly like or follow, your like or follow is my motivation to write down.

Let’s make progress on the road together ~🤭