What is the type of “keys” in JavaScript?

2020-02-24 12:29发布

问题:

I bumbed into one of those moments when I just lose the focus and start wondering on a silly question:

var a = {
  b: "value"
}

What is the typeof 'b' and I don't mean the typeof "value", but the actual Key labeled as b?

background: I started wondering about this when I had to create a key which is a string:

var a = {
  "b": "value"
}

because at a later point it is referenced as:

a["b"]

And then eneded up wondering the original question.

回答1:

In object literal terms, b is a property. Properties are either strings or symbols in JavaScript, although when defining the property name inside an object literal you may omit the string delimiters.

for (key in a) {
    alert(typeof key);
    //-> "string"
}


回答2:

Property names are automatically coerced into a string. You can try this yourself by using a numeric literal as a property name.

var object = {
  .12e3: 'wut'
};
object[.12e3]; // 'wut'
object['.12e3']; // undefined
object['120']; // 'wut'

// Let’s try another numeric literal:
object = {
  12e34: 'heh'
};
object[12e34]; // 'heh'
object['12e34']; // undefined
object[1.2e35]; // 'heh'
object['1.2e35']; // undefined
object[1.2e+35]; // 'heh'
object['1.2e+35']; // 'heh'

For this reason, I’d recommend using only string literals for property names.

From Unquoted property names / object keys in JavaScript, my write-up on the subject:

Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.

[…]

Bracket notation can safely be used for all property names.

[…]

Dot notation can only be used when the property name is a valid identifier name.

I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.



回答3:

b is a string, it's just a shorthand syntax, so you write

var a = {
    b: "value"
}

instead of

var a = {
  "b": "value"
}


回答4:

Keep in mind that JavaScript objects are hash tables and the keys are just strings. You may omit the quotes around property names during declaration, but if you use reserved words for property names or any other name that happens to be an invalid identifier, such as starting with a digit, or containing spaces, you would have to wrap the property names in quotes:

var a = {
  "1b":       "value",
  "b and c":  "value",
  "+12345":   "value"
};

Also note that you can reference the properties of objects using the dot notation or the subscript notation regardless of whether quotes where used when they were declared. However, if you use property names that would be invalid identifiers, such as the ones in the above example, you are forced to use the subscript notation:

a.1b             // invalid (dot notation)
a["b and c"];    // valid   (subscript notation)


回答5:

var a = {$ : 'hello', 2123 : 'number'};
for(var key in a) {
  console.log(typeof key)
}

Keys in javascript objects can be strings and symbols. symbol is a primitive data type in javascript.