Javascript - Class ยท Object

< Class >

  • object(์ธ์Šคํ„ด์Šค)๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํ‹€์ด๋‹ค.
  • template(ํ‹€๋งŒ์„ ์ •์˜ํ•ด์„œ ํ•œ๋ฒˆ๋งŒ ์„ ์–ธํ•œ๋‹ค.)
  • class ์ž์ฒด์— data๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š๋‹ค.
  • class๋Š” ์ •์˜๋งŒ ํ•œ ๊ฒƒ์ด๋ผ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์—๋Š” ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

1. Class ์„ ์–ธ

class Person {
  //constructor (์ƒ์„ฑ์ž)
  constructor(name, age) {
    //fields
    this.name = name;
    this.age = age;
  }

  //methods
  speak() {
    console.log(`${this.name}: hello!`);
  }
}

const hyesung = new Person("hyesung", 24);
console.log(hyesung.name);
console.log(hyesung.age);
hyesung.speak();

2. Getter and Setters

๐Ÿ’ก ์‚ฌ์šฉ์ž๊ฐ€ class๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•ด๋„ ๋ฐฉ์–ด์ ์ธ ์ž์„ธ๋กœ ๋งŒ๋“ค์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ฒƒ

class User {
  constructor(firstName, lastName, age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age; //getter์™€ setter๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ getter,setter ํ˜ธ์ถœ
  }

  get age() {
    return this._age; //๊ธฐ์กด์˜ age์‚ฌ์šฉ์‹œ ๋ฌดํ•œํ˜ธ์ถœ
  }

  set age(value) {
    this._age = value < 0 ? 0 : value; // ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’ ๊ฒ€์‚ฌ
  }
}

const use1 = new User("Steve", "Job", -1); //๋‚˜์ด๋ฅผ -1๋กœ ์ž˜๋ชป ์„ค์ •์‹œ
console.log(use1.age);

3. Inheritance (์ƒ์†)

  • ํ•œ ํด๋ž˜์Šค์—์„œ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋กœ ํ™•์žฅํ•˜๊ณ ์ž ํ•  ๋•Œ
  • ๊ณตํ†ต๋˜์–ด์ง€๋Š” ๊ฐ’๋“ค์„ extends ํ‚ค์›Œ๋“œ๋กœ ์žฌ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค

4. Polymorphism(๋‹คํ˜•์„ฑ)

  • ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค(์˜ค๋ฒ„๋ผ์ด๋”ฉ)
  • ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•œ ์ƒํƒœ์—์„œ ๋ถ€๋ชจ class์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด super ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉ
class Shape {
  constructor(width, height, color) {
    this.width = width;
    this.height = height;
    this.color = color;
  }

  draw() {
    console.log(`drawing ${this.color} color!`);
  }

  getArea() {
    return this.width * this.height;
  }
}
class Rectangle extends Shape {} //์ƒ์†์„ ํ†ตํ•ด ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
class Triangle extends Shape {
  draw() {
    super.draw(); //super : ์žฌ์ •์˜ ์‹œ, ๋ถ€๋ชจ์˜ method๊นŒ์ง€ ํ˜ธ์ถœ
    console.log("๐Ÿ”บ");
  }
  //๋‹คํ˜•์„ฑ : ํ•„์š”ํ•œ ํŠน์ •ํ•จ์ˆ˜๋งŒ ์žฌ์ •์˜
  getArea() {
    return (this.width * this.height) / 2;
  }
}
const rectangle = new Rectangle(20, 20, `Green`);
rectangle.draw();
console.log(rectangle.getArea());
const triangle = new Triangle(20, 20, `Blue`);
triangle.draw();
console.log(triangle.getArea());

5. Class cheking: instanceOf

  • instanceof ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๋น„๊ต
console.log(rectangle instanceof Rectangle); //True
console.log(triangle instanceof Rectangle); //False
console.log(triangle instanceof Triangle); //True
console.log(triangle instanceof Shape); //True
console.log(triangle instanceof Object); //True

< Object >

  • object = { key : value }
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด๋Š” key์™€ value๋กœ ๊ตฌ์„ฑ๋œ property์˜ ์ง‘ํ•ฉ
  • key๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜์ด๊ณ , value๋Š” ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’

1. Literals and properties

  • โ€˜object literalโ€™ syntax : ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ object์ƒ์„ฑ
const obj1 = {};

-โ€˜object constructorโ€™ syntax : ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ object ์ƒ์„ฑ

const obj2 = new Object();
//object๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ,
const name = "hyesung";
const age = 25;
print(name, age);
function print(name, age) {
  console.log(name);
  console.log(age);
}

//object๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ,
const hyesung = { name: "hyesung", age: 25 };
function print1(person) {
  console.log(person.name);
  console.log(person.age);
}
print(hyesung);

2.Computed(๊ณ„์‚ฐ๋œ) properties

  • ๋™์ ์œผ๋กœ key, value๋ฅผ ๋ฐ›์•„์™€์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ
  • ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ .๋ง๊ณ ๋„ ๋ฐฐ์—ด์—์„œ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๋“ฏ []์‚ฌ์šฉ๊ฐ€๋Šฅ
  • ๋‹จ, [] ์‚ฌ์šฉ์‹œ string์œผ๋กœ ํ‘œํ˜„
console.log(hyesung.name); //์ฝ”๋”ฉํ•˜๋Š” ์ˆœ๊ฐ„ key์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์„ ๋•Œ
console.log(hyesung["name"]); //์ •ํ™•ํžˆ ์–ด๋–ค key๊ฐ€ ํ•„์š”ํ•œ์ง€ ๋ชจ๋ฅผ ๋•Œ(runtime์—์„œ ๊ฒฐ์ •๋  ๋•Œ)
hyesung["hasJob"] = false;
console.log(hyesung.hasJob);

function printValue(obj, key) {
  // console.log(obj.key); ํ‚ค ๊ฐ’์ด ์˜ค๋ธŒ์ ํŠธ์— ์ •์˜ x
  console.log(obj[key]); //์ง€์ •๋˜์ง€ ์•Š์€ ์˜ค๋ธŒ์ ํŠธ๋ฅผ key๊ฐ€ ํ‘œํ˜„
}

printValue(hyesung, "name");
printValue(hyesung, "age");

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

  • makePerson์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฐ’๋งŒ ์ „๋‹ฌํ•˜๋ฉด object๋ฅผ ๊ฐ„๋‹จํžˆ ์ƒ์„ฑ๊ฐ€๋Šฅ
const person1 = { name: "bob", age: 2 };
const person2 = { name: "steve", age: 3 };
const person3 = { name: "dave", age: 4 };
const person4 = makePerson("john", 30);
console.log(person4);

function makePerson(name, age) {
  return {
    name, //key์™€ value์˜ ์ด๋ฆ„์ด ๋™์ผํ•˜๋‹ค๋ฉด ์ƒ๋žต๊ฐ€๋Šฅ
    age,
  };
}

4. Constructor Function (์ƒ์„ฑ์ž ํ•จ์ˆ˜)

function Person(name, age) {
  //this = {}; ํ‘œํ˜„์„ ์ƒ๋žต
  this.name = name;
  this.age = age;
  //return this; ํ‘œํ˜„์„ ์ƒ๋žต
}
  • ์ˆœ์ˆ˜ํ•˜๊ฒŒ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ  return ์“ฐ์ง€ ์•Š๊ณ  class์—์„œ constructor ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ this., ํ˜ธ์ถœ์„ new Person ์ฒ˜๋Ÿผ ํ•œ๋‹ค.

5. in operator : property existence check (key in obj) ํ•ด๋‹นํ•˜๋Š” object ์•ˆ์— key ์œ ๋ฌด

console.log("name" in hyesung);
console.log("age" in hyesung);
console.log("random" in hyesung);
console.log(hyesung.random);

6. for..in vs for..of

  • for (key in obj)
for (let key in hyesung) {
  console.log(key);
}
  • for (value of iterable) ๋ฐฐ์—ด, ๋ฆฌ์ŠคํŠธ์—์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๊ฒƒ์—์„œ ์‚ฌ์šฉ
//์ด์ „
for(let i = 0; i < array.lenght; i++) {
  console.log(array[i];);
}
// for of ์‚ฌ์šฉ
const array = [1, 2, 4, 5];
for(let value of array) {
    console.log(value);
}

7. Object.assign(dest, [obj1, obj2, obj3โ€ฆ])

  • object ์ž์ฒด๋ฅผ ๋ณต์‚ฌ
const user = { name: "popo", age: 5 };
const use2 = user;
use2.name = "coder";
console.log(user);

//๋ฐฉ๋ฒ•1
const user4 = {};
Object.assign(user4, user);
console.log(user4);

//๋ฐฉ๋ฒ•2
const user5 = Object.assign({}, user);
console.log(user5);

์ถœ์ฒ˜ : ๋“œ๋ฆผ์ฝ”๋”ฉ by ์—˜๋ฆฌ https://youtu.be/_DLhUBWsRtw https://youtu.be/1Lbr29tzAA8