๐Ÿ‘พ TIL) JavaScript์˜ Object

Solmiiยท2020๋…„ 5์›” 6์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
9/24
post-thumbnail

Object๋ž€?

Primitive type์€ ๋ณ€์ˆ˜ ํ•˜๋‚˜๋‹น ๊ฐ’์„ ํ•˜๋‚˜๋งŒ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.

const name = "solmi";
const age = 26;

print(name, age);

function print(name,age) {
 ๋Œ€์ถฉ ํ•จ์ˆ˜
}

๊ทธ๋ž˜์„œ ์ด๋ ‡๊ฒŒ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๊ฐ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•˜๊ณ , ํ•จ์ˆ˜๋„ ๊ทธ๋งŒํผ ๊ธธ์–ด์ง„๋‹ค.

โ‡’ ๊ด€๋ฆฌ๋„ ํž˜๋“ค๊ณ  ๊ฐ€๋…์„ฑ๋„ ์ข‹์ง€ ์•Š๋‹ค! ๊ทธ๋ž˜์„œ ์‚ฌ์šฉ๋˜๋Š”๊ฒƒ์ด ๋ฐ”๋กœ Object!!


1. Literals properties

object๋Š” { key : value } ๋กœ ๊ตฌ์„ฑ๋˜์–ด์ง„๋‹ค!

object literal

const solmi = { name: "solmi", age: 26 };
print(solmi);

{ }๋ฅผ ์ด์šฉํ•ด object๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
dot notation .์œผ๋กœ ์†์„ฑ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

Javascript๋Š” dynamic typed language์ด๋‹ค.
๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋Š” ์ผ๋ฐ˜์ ์ด์ง€ ์•Š์€ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์ด ์ผ์–ด๋‚œ๋‹ค.

ํ•œ ์˜ˆ๋กœ, object๋ฅผ ์ •์˜ํ•œ ํ›„์— ๋‹ค๋ฅธ ๊ฐ’์„ ๋’ค๋Šฆ๊ฒŒ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜๋„ ์žˆ๋‹ค!

solmi.likeChicken = true; โ‡’ solmi object๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด solmi, 26, true ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
delete solmi.age; โ‡’ solmi object๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด solmi, true ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

๐Ÿ’๐Ÿปโ€โ™€๏ธ trailing comm

๋ง ๊ทธ๋Œ€๋กœ ์ฝค๋งˆ , ๋ฅผ ๋งˆ์ง€๋ง‰์— ๋ถ™์ด๋Š”๊ฑธ ์˜๋ฏธํ•œ๋‹ค.
๋‹ค๋ฅธ ์–ธ์–ด๋Š” ์—๋Ÿฌ๊ฐ€ ๋œจ๊ฒ ์ง€๋งŒ JavaScript์—์„œ๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค.
object์˜ property ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉํ•˜์ง€๋งŒ, ์‚ฌ์šฉํ•˜๊ณ  ์•ˆํ•˜๊ณ ๋Š” ์ทจํ–ฅ์˜ ์ฐจ์ด์ด๋‹ค.
๊ฐ€์žฅ ์ค‘์š”ํ•œ๊ฑด ํ˜‘์—…์‹œ์˜ ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜์ด๋‚˜ ์ฝ”๋“œ์˜ ํ†ต์ผ์„ฑ์„ ์ง€ํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.


object constructor

const solmi = new Object();

new ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑ์ž(constructor)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.


2. Computed properties

solmi.name; // ์ด๋ ‡๊ฒŒ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜
solmi["name"]; // ์ด๋ ‡๊ฒŒ ํ˜ธ์ถœ ๊ฐ€๋Šฅ!

Object.keys(solmi); // solmi ์˜ค๋ธŒ์ ํŠธ์˜ key ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
Object.Values(solmi); // solmi ์˜ค๋ธŒ์ ํŠธ์˜ value ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

[solmi.name;](http://solmi.name) ์ด ํ›จ์”ฌ ํŽธํ•  ๊ฒƒ ๊ฐ™์€๋ฐ, [ ] ๋Š” ์–ธ์ œ ์“ธ๊นŒ?

[ ] ์•ˆ์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค!

let plan1 = {   
  name: "Basic"
};
let propertyName = "name";
console.log(plan1[propertyName]); // Basic

โ‡’ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ name์ด๋ผ๋Š” property์— ์ด๋ ‡๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

object["key"]

const solmi = { name: "solmi", "a-2": 1234, "  ": 5678 };

console.log(solmi.name); // solmi
console.log(solmi["name"]); // solmi
console.log(solmi[name]); // undefined
console.log(solmi.a-2); // NaN
console.log(solmi."a-2"); // error
console.log(solmi["a-2"]); // 1234
console.log(solmi["  "]); // 5678

object ์•ˆ์— ์žˆ๋Š” ๊ฐ’์— ์ ‘๊ทผํ• ๋•Œ๋Š” . ์„ ์ด์šฉํ–ˆ๋Š”๋ฐ, [" "]์„ ์ด์šฉํ•ด์„œ string ํ˜•ํƒœ๋กœ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด "a-2", "123", " " ๊ฐ™์€ propertie๋Š” . ์œผ๋กœ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ undefined ํ˜น์€ error ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๋•Œ object["key"] ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’์ด " "์œผ๋กœ ๊ฐ์‹ธ์ง„ ๋ฌธ์ž์—ด์ด๊ธฐ์— ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

object ["key"]๋ฅผ ์ด์šฉํ•ด์„œ๋„ ๋’ค๋Šฆ๊ฒŒ ์ถ”๊ฐ€, ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

solmi["longHair"] = true;
delete solmi["name"];


object[key]

  • . : ์ฝ”๋”ฉํ•˜๋Š” ๊ทธ ์ˆœ๊ฐ„, Key์— ์ •ํ™•ํžˆ ํ•ด๋‹นํ•˜๋Š” vlaue๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉ โ‡’ ํ‰์†Œ์—๋Š” . ์„ ์‚ฌ์šฉ
  • [ ] : ์ •ํ™•ํ•˜๊ฒŒ ์–ด๋–ค key๊ฐ€ ํ•„์š”ํ•œ์ง€ ๋ชจ๋ฅผ ๋•Œ, ์ฆ‰ vlaue๊ฐ€ ๋Ÿฐํƒ€์ž„์—์„œ ๊ฒฐ์ •๋  ๋•Œ ์‚ฌ์šฉ
function printValue(obj, key) { // key : ์‚ฌ์šฉ์ž์—๊ฒŒ input์œผ๋กœ ์ „๋‹ฌ๋ฐ›์„ ์ •๋ณด(์•„์ง ์–ด๋–ค ์ •๋ณด์ธ์ง€๋Š” ๋ชจ๋ฆ„)
  console.log(obj.key); // undefuned
  console.log(obj[key]; // solmi
}

printValue(solmi, "name");

์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„ ํ•จ์ˆ˜์—์„œ key ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌ๋ฐ›์„ ์˜ˆ์ •์ธ ์ •๋ณด๋ผ๋ฉด, ๋‹น์žฅ ์ง€๊ธˆ์€ ์ •ํ•ด์ง„ value๊ฐ€ ์—†๊ฒŒ ๋œ๋‹ค!

์ด ๋•Œ, obj.[key] ๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋‚˜์ค‘์— ๊ฐ’์ด ๋“ค์–ด์˜ค๊ฒŒ๋˜๋ฉด key์˜ ์†์„ฑ๊ฐ’์ด ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค!

๋งŽ์ด ์“ฐ์ด๋Š” ๊ฐ์ฒด ๊ตฌ์กฐ! ๊ฐ์ฒด์˜ ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์˜ ๊ฐ์ฒด.....๐Ÿคช?

let objData = {
  name: 50,
  address: { 
    email: "gaebal@gmail.com",
    home: "์œ„์›Œํฌ ์„ ๋ฆ‰2ํ˜ธ์ "
  },
  books: {
    year: [2019, 2018, 2006],
    info: [{
      name: "JS Guide",
      price: 9000
    }, {
      name: "HTML Guide",
      price: 19000,
      author: "Kim, gae bal"
    }]
  }
};
let bookName = objData.books.info[1].name; // bookName๋ณ€์ˆ˜๋Š” objData๊ฐ์ฒด ์•ˆ์— books๋ผ๋Š” ๊ฐ์ฒด ์•ˆ์— info ๋ฐฐ์—ด์˜ 1๋ฒˆ index์œ„์น˜์ธ ๊ฐ์ฒด ์•ˆ์— name property!
console.log(bookName); // HTML Guide

3.Property shorthand (๋‹จ์ถ• ์†์„ฑ๋ช…)

const name = "What's your name?", age = "How old are you?";

const person1 = { name: "What's your name?", age: "How old are you?" };
const person2 = { name, age };

๋‹จ์ถ• ์†์„ฑ๋ช…์€ key ๊ฐ’๊ณผ vlaue์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด, key, value์˜ ๊ฐ’์„ ๊ฐ๊ฐ ํ‘œ๊ธฐํ•˜์ง€ ์•Š๊ณ  ํ•œ๋ฒˆ๋งŒ ํ‘œ๊ธฐํ•˜๋Š”๊ฒƒ์„ ๋งํ•œ๋‹ค.

person2 ๋Š” { name, age }๋งŒ ์ž…๋ ฅํ–ˆ์ง€๋งŒ, ์‚ฌ์‹ค์€ { name: name, age: age } ์ธ๊ฒƒ! (์ฝ˜์†”์ฐฝ์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค!)


4. Constructor function

function Person(name, age) { // object๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋ณ€์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”๊ฒŒ ๊ด€๋ก€
  // ์‚ฌ์‹ค์€ this = {}; ๊ฐ€ ์ƒ๋žต๋œ๊ฒƒ
  this.name = name;
  this.age = age;
  // ์‚ฌ์‹ค์€ return this; ์ด ์ƒ๋žต๋œ๊ฒƒ
}

const solmi = new Person("solmi", 26);

Class๋Š” ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์ด ์•„๋‹Œ, ๊ธฐ์กด์— ์กด์žฌํ•˜๋˜ ํ”„๋กœํ† ํƒ€์ž…์„ ๊ธฐ๋ฐ˜์œผ๋กœ OOP์— ์นœ์ˆ™ํ•œ ๋ชจ์–‘์œผ๋กœ ๋ฌธ๋ฒ•๋งŒ ๋ฐ”๋€ syntactic sugar ๋ผ๊ณ  ๋งํ–ˆ์—ˆ๋Š”๋ฐ, Constuctor function๊ณผ prototypal inheritance๊ฐ€ ๊ทธ ํ”„๋กœํ† ํƒ€์ž…์— ํ•ด๋‹นํ•œ๋‹ค.

class๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „์— ๋งŽ์ด ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ์‹์ด๋ฉฐ, ๋‹ค๋ฅธ ๊ณ„์‚ฐ์„ ํ•˜์ง€ ์•Š๊ณ  ์ˆœ์ˆ˜ํ•˜๊ฒŒ object๋งŒ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค.

class๋‚˜ Constuctor function๋“ฑ์€ ๊ฒ‰๋ณด๊ธฐ์—๋งŒ ๋‹ค๋ฅผ๋ฟ, ์‹ค์ œ ๋’ท๋‹จ์—์„œ ๋Œ์•„๊ฐ€๋Š”๊ฒƒ์€ ๋˜‘๊ฐ™๋‹ค.
(prototypal inheritance๋Š” ๋‹ค์Œ์— ์ •๋ฆฌ...)


5. in operator

console.log("name" in solmi); // true
console.log("job" in solmi); // false
console.log(solmi.job); // undefined

object์•ˆ์— ํ•ด๋‹น key๊ฐ€ ์žˆ๋Š”์ง€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™•์ธํ•˜๋Š” ํ‚ค์›Œ๋“œ


6. Cloning

const user = { name: "solmi", age: "26" };
const user2 = user;

โ‡’ user2 = user ๊ฐ™์€ ํ—ˆ์ ‘ํ•œ ๋ฐฉ๋ฒ• ๋ง๊ณ  object๋ฅผ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–ค๊ฒƒ๋“ค์ด ์žˆ์„๊นŒ??

์˜›๋‚  st

const user3 = {}; // ๋นˆ ๋ฐฐ์—ด ์ƒ์„ฑ
for (key in user) {
  user3[key] = user[key];
}

๋นˆ user3 ์•ˆ์— ๋ณต์‚ฌํ•˜๊ณ ์ž ํ•˜๋Š” user์˜ ๊ฐ’์„ for..in์œผ๋กœ ๊ณ„์† ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ๋„ฃ์–ด์ฃผ๊ธฐ

์š”์ฆ˜ st

const user4 = {}; // ๋นˆ ๋ฐฐ์—ด ์ƒ์„ฑ
Object.assign(user4, user);
const user4 = Object.assign({}, user); // ์ด๋ ‡๊ฒŒ ํ•œ์ค„๋กœ๋„ ์ž‘์„ฑ ๊ฐ€๋Šฅ ( ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋‚ด์šฉ)

Object.assign(target, source) : ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ์ถœ์ฒ˜ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ๋Œ€์ƒ ๊ฐ์ฒด๋กœ ์†์„ฑ์„ ๋ณต์‚ฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ , ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๊ฑด MDN ๊ณต์‹ ์„ค๋ช…์ด๊ณ , ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๊ฐ์ฒด ๋ณ‘ํ•ฉ์ด๋‹ค.

user์„ ๋ณต์‚ฌํ•˜์—ฌ user4์— ๋ถ™์—ฌ๋„ฃ์€๊ฒƒ ๊ฐ™์ง€๋งŒ, ์‚ฌ์‹ค์€ user์™€ user4๊ฐ€ ๋ณ‘ํ•ฉ๋œ๊ฒƒ์ด๋ฉฐ, ๋ณ‘ํ•ฉ๋œ ๋Œ€์ƒ์ธ user4๊ฐ€ ๋ฐ˜ํ™˜๋œ๊ฒƒ!
๊ทธ๋ ‡๊ธฐ์— ๋ณต์‚ฌ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๋ฐ˜๋“œ์‹œ ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค!

๋˜, ๊ฐ์ฒด ๋ณต์‚ฌ๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ๋ณ‘ํ•ฉ์ธ ์ด์œ ๋Š”,

const fruit1 = { color: "red" };
const fruit2 = { color: "blue", aize: "big" }
const mixed = Object.assign({}, fruit1, fruit2);
// mixed์˜ color: blue, size: big ์ถœ๋ ฅ

์ด ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋‹ค!

๋’ค์— ๋‚˜์˜จ ๊ฐ์ฒด (fruit2) ์ผ์ˆ˜๋ก, ์•ž์— ๋™์ผํ•œ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ๊ฐ’์„ ๋ฎ์–ด์”Œ์›Œ ๋ฒ„๋ฆฐ๋‹ค.

profile
ํ•˜๋ฃจ๋Š” ์น˜์—ดํ•˜๊ฒŒ ์ธ์ƒ์€ ์—ฌ์œ ๋กญ๊ฒŒ

0๊ฐœ์˜ ๋Œ“๊ธ€