Object (객체) is what I learned today in javascript. Objects consist a key and a value. Which is a better way than array when in different data sets where there are same groups of keys but different values. Uh. I am really bad in explaining what object is a javascript. I will use an example.
firstName = 'Inseo',
lastName = 'Park';
firstName2 = 'Zac',
lastName2 = 'Lee';
Now if this is a data set, and you have to process more than a hundred first names and last names, this will lead to a too much of a data which makes using object as a type of data very efficient. Using object,
You can tie each data using a user name.
User1 = {
firstName = 'Inseo',
lastName = 'Park'
}
User2 = {
firstName = 'Zac',
lastname = 'Park'
}
This way it is much more efficient and you can add different keys and values into the User you want to and process each user and compare it to another user.
There are two different ways you can approach a value in an object.
A. Dot notation
Dot notation ex. user.firstName; // 'Inseo'
user2.firstName; // 'Zac'
B. Bracket notation
Bracket notation ex. user['firstName'] // 'Inseo'
user2['firstName'] // 'Zac'
Now the difference between this two is that you will have a certain situation where you have to use bracket notation. When the 'key name' changes from time to time, you have to use bracket notation. For example, when you want to make a function that continously finds the value in a variable key, you have to use bracket notation to make the function usable in any situation.
If user[variableKeyName] is used, then when variableKeyName = firstName at first and lastName at second. Using the bracket notation will lead to the function to find 'Inseo' at first and 'Park' at the second attempt. Simply, you have to use bracket notation when the key name varies from time to time.
You can add value in an object by using
ex. user['age'] = '39' // user { firstName = 'Inseo', lastName = 'Park', age = '39' }
You can delete value in an object
ex. delete user.firstName;
delete user['age'];
You can check if an object contains a key.
ex. 'content' in user // false
'age' in user // true
When you meet an error while programming or coding, do not just ignore it but try to read over it.
Life will give you failures and pain but don't put any mind on that, without any failures you will not become happy nor successful
Being thankful for really little things is a key to moving on everyday with happiness.
화이팅입니다!