Today, I'm going to organize what I've studied over the past few days.
There is a book called 'The Modern JS Deep Dive.'
This is one of the most famous books about JS in Korea, so I chose it.
so What I studied?
It is about object literals.
First of all, before explaining the types of objects, we should understand what an 'object' is.
It is a combination of property keys and values.
Ex)
var p = {
name: 'lee',
age: 8
}
In this case, `name` is the key, and 'lee' is the value.
Types of objects
There are two types:
one is a reference type, and the other is a primitive type.
A primitive type is an immutable value, so we can't modify the value after assigning it to an object.
Ex)
const a = 10;
a = 20;
On the surface, it seems like the value is changed, but it isn't.
Actually, the value is saved in memory, so even if you try to change something in a primitive type, the original value remains unchanged, and only the reference to the new value is updated.
And you can think of a reference type as being more flexible.
You can write a method as part of a reference type's object.
Ex)
var p = {
name: 'lee',
sayHello: function() {
console.log(`hello! my name is ${this.name}`);
}
}
As you can see, there is a function within the `p` object.
Rules for creating keys and values
And there are some rules for creating keys and values.
1. If you want to make a string value, you should use double quotes ("") or single quotes ('').
2. You can't use '-' in a key without quotes. For example, 'last-name' should be written as `lastName` or 'last-name'.
3. You can use words like `var`, `function`, etc. as keys, but it can cause issues, so it's better not to use them.
4. If you use the same key in an object, it's like a duplicate, so the last value will overwrite the original one.
Ex)
var foo = {
name: 'kim',
name: 'lee'
}
console.log(foo); // {name :'lee'}
how we can access properties??
You can access a value like an array using bracket notation.
If the key is not a number, you should use quotes ('') to access the value.
Ex)
var foo = {
name: 'kim'
};
console.log(foo.name); // or console.log(foo['name']);
If the key is a number, we can access the value in two ways:
1. `foo[1]`
2. `foo['1']`
You don't need to use quotes ('') when accessing a numerical key.
'열정가득한 개발자의 이야기 > 한땀 한땀 공부 내용 TIL' 카테고리의 다른 글
TIL_ React 공부 (4) | 2024.09.03 |
---|---|
TIL 스레드가 뭐지? (0) | 2024.08.20 |
공부한거 정리 (0) | 2024.07.01 |
React component (부모 컴포넌트란?!) (0) | 2024.06.19 |
React anti-pattern (0) | 2024.06.19 |