๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Javascript

Class ํด๋ž˜์Šค

Class 

์ž๋ฐ”์ŠคํฌํŒํŠธ๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์˜ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.

static method or static properties

  • ํ”„๋กœํ„ฐํƒ€์ž…์— ํ• ๋‹น๋˜์ง€ ์•Š๊ณ  ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๊ฐ์ฒด์— ์ง์ ‘ ํ• ๋‹น๋˜์–ด์žˆ๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋ฅผ
    static method, static properties๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž ์—†์ด ํ•จ์ˆ˜๋กœ์จ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

prototype method

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ”„๋กœํ† ํƒ€์ž… ๋‚ด๋ถ€์— ํ• ๋‹น๋œ ๋ฉ”์†Œ๋“œ๋“ค์„ prototype method๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์†

  • ์ƒ์†์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ES5

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ํ”„๋กœํ† ํƒ€์ž…, ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
/**
 * super class
 */
function Animal(type, name, gender) {
  this.type = type;
  this.name = name;
  this.gender = gender;
}

/**
 * static method
 */
Animal.getInfo = function (instance) {
  return {
    type: instance.type,
    name: instance.name,
    gender: instance.gender,
  };
};

/**
 * prototype method
 */
Animal.prototype.getType = function () {
  return this.type;
};

Animal.prototype.getName = function () {
  return this.name;
};

Animal.prototype.getGender = function () {
  return this.gender;
};

/**
 * sub class
 */
function Dog(name, gender) {
  this.type = "dog";
  this.name = name;
  this.gender = gender;
}

/**
 * ์ƒ์†
 */
Dog.prototype = new Animal(); // ๊ธฐ์กด์— ์žˆ๋˜ prototype ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— Dog์˜ constructor๋Š” Animal์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
Dog.prototype.constructor = Dog; // ๋‹ค์‹œ ๊ธฐ์กด์˜ constructor๋ฅผ ๋‹ค์‹œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
Dog.prototype.bark = function () {
  // Dog์˜ ํ”„๋กœํ† ํƒ€์ž…์€ ์ƒˆ๋กœ์šด ํ”„๋กœํ† ํƒ€์ž…์„ ๋ฎ์–ด ์”Œ์šด ๋‹ค์Œ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  console.log("wang wang");
};

/**
 * Animal์˜ static properties(type, name, gender)๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” prototype์„ ์ƒ์† ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค.
 */
function Bridge() {}
Bridge.prototype = Animal.prototype;
Dog.prototype = new Bridge();
Dog.constructor = Dog;
Dog.prototype.bark = function () {
  // Dog์˜ ํ”„๋กœํ† ํƒ€์ž…์€ ์ƒˆ๋กœ์šด ํ”„๋กœํ† ํƒ€์ž…์„ ๋ฎ์–ด ์”Œ์šด ๋‹ค์Œ ์ •์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  console.log("wang wang");
};

/**
 * instance
 */
var dog = new Animal("dog", "tom", "male");

dog.getName(); // tom
Animal.getInfo(dog); // {type: 'dog', name: 'tom', gender: 'male'}

var kong = new Dog("kong", "female");
console.dir(kong);

ES6

  • class ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํด๋ž˜์Šค๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
/**
 * super class
 */
class Animal {
  constructor(type, name, gender) {
    this.type = type;
    this.name = name;
    this.gender = gender;
  }
  /**
   * static method
   */
  static getInfo(instance) {
    return {
      type: instance.type,
      name: instance.name,
      gender: instance.gender,
    };
  }

  /**
   * prototype method
   */
  getType() {
    return this.type;
  }
  getName() {
    return this.name;
  }
  getGender() {
    return this.gender;
  }
}

/**
 * sub class
 */
class Dog extends Animal {
  constructor(name, gender) {
    super("dog", name, gender); // ๋ถ€๋ชจ ํด๋ž˜์Šค ์ƒ์„ฑ์ž ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  }
  bark() {
    console.log("wang wang");
  }
}

/**
 * instance
 */
var dog = new Animal("dog", "tom", "male");

dog.getName(); // tom
Animal.getInfo(dog); // {type: 'dog', name: 'tom', gender: 'male'}

var kong = new Dog("kong", "female");
console.dir(kong);

'Javascript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๊ฐ์ฒด ์ง€ํ–ฅ Prototype  (0) 2022.12.04
Javascript This  (0) 2022.11.21
์‹คํ–‰ ์ปจํ…์ŠคํŠธ  (0) 2022.11.13
๋ฐ์ดํ„ฐ ํƒ€์ž…(์›์‹œํ˜• vs ์ฐธ์กฐํ˜•)  (0) 2022.11.01