The first question
let a = {}, b = '0', c = 0;
a[b] = 'Everest';
a[c] = 'training';
console.log(a[b]); / / -- > training
Copy the code
Resolution:
Heap: Space for storing reference type values
Stack: An environment in which values of primitive types are stored and code is executed
Key points:
1. Attribute names of objects cannot be repeated
2. Numeric attribute name === String attribute name
Extension problem
1. What is the difference between objects and arrays?
A: Objects and arrays can both be used to represent collections of data. There are some obvious differences between them:
- An array is an ordered data set, and an object is an unordered data set.
- Array data does not have names, but object data has names (property names)
- Both can use subscripts to read values, but the subscripts of data can only be numbers, while the subscripts of objects can be strings or symbols
The second question
let a={},b=Symbol('1'),c=Symbol('1');
a[b]='Everest';
a[c]='training';
console.log(a[b]); / / -- > mount Everest
Copy the code
Symbol is a global function that creates unique values
Important: The attribute name of an object is not necessarily a string, but may also be a Symbol value
The third question
let a = {},
b = {
n:'1'
},
c = {
m:'2'
};
a[b] = 'Everest';
a[c] = 'training';
console.log(a[b]); / / -- > training
Copy the code
Key words:
Both primitive types and symbols can be used as object attribute names, but numeric and string attributes use a single attribute
2. When a value of a reference type is used as an object property name, the reference type is converted to a string. All reference types are converted to strings: ‘object object ‘