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 |