티스토리 뷰
728x90
반응형
객체 생성자
function Animal( type, name, sound ) {
this.type = type;
this.name = name;
this.sound = sound;
this.say = function() {
console.log(this.sound);
};
}
const dog = new Animal('개', '슈슈', '슈슝~');
const cat = new Animal('고양이', '줍줍', '줍줍이~');
dog.say(); //슈슝~
cat.say(); //줍줍이~
prototype
prototype 은 동일한 클래스인 객체들끼리 값이나 함수를 공유할 수 있다.
function Animal( type, name, sound ) {
this.type = type;
this.name = name;
this.sound = sound;
}
// prototype으로 say 함수를 공유
Animal.prototype.say = function () {
console.log(this.sound);
}
// prototype으로 sharedValue 값을 공유
// 모든 클래스마다 sharedValue 필드에는 1값이 초기화 되어있다.
Animal.prototype.sharedValue = 1 ;
const dog = new Animal('개', '슈슈', '슈슈~');
const cat = new Animal('고양이', '짝귀', '애옹~');
dog.say(); // 슈슈~
console.log(dog.sharedValue); // 1
cat.say(); // 애옹~
console.log(cat.sharedValue); // 1
dog.sharedValue = 24;
console.log(dog.sharedValue); // 24
const dog2= new Animal('개', '하키솜', '솜솜~');
dog2.say(); // 솜솜~
console.log(dog2.sharedValue); // 1
객체 상속
- call을 이용하여 상속된 객체를 불러온다
function Animal( type, name, sound ) {
this.type = type;
this.name = name;
this.sound = sound;
}
Animal.prototype.say = function () {
console.log(this.sound);
}
// 상속된 객체 정의
function Dog( name, sound ) {
Animal.call(this, '개', name, sound );
}
function Cat( name, sound ) {
Animal.call(this. '고양이', name, sound );
}
Dog.prototype = Animal.prototype;
Cat.prototype = Animal.prototype;
const dog = new Dog('슈슈', '왕왕왕');
const cat = new Cat('줍줍이', '액옹!');
dog.say(); // 왕왕왕
cat.say(); // 액옹!
ES6 클래스 ( Java 클래스 생성과 매우 유사!)
- 동물 예제
class Animal {
constructor( type, name, sound ) {
this.type = type;
this.name = name;
this.sound = sound;
}
say() {
console.log(this.sound);
}
}
const dog = new Animal('개', '슈슈', '왕왕');
const cat = new Animal('고양이', '짝귀' '애옹');
dog.say(); // 왕왕
cat.say(); // 애옹
- 동물예제 - 상속이 있는 경우
class Animal {
constructor( type, name, sound ){
this.type = type;
this.name = name;
this.sound = sound;
}
say() {
console.log(this.sound);
}
}
class Dog extends Animal {
constructor(name, sound){
super('개', name, sound); // 부모클래스 Animal의 생성자를 호출.
}
}
class Cat extends Animal {
constructor(name, sound){
super('고양이', name, sound); // 부모클래스 Animal의 생성자를 호출.
}
}
const dog2 = new Dog('하키솜', '솜솜');
const cat2= new Cat('줍줍이', '액옹');
dog2.say(); //솜솜
cat2.say(); //액옹
- 음식 예제
class Food {
constructor(name) {
this.name = name;
this.brands = [];
}
addBrand(brand) {
this.brands.push(brand);
}
print() {
console.log(`${this.name} (를)을 파는 음식점들 : `);
console.log(this.brands.join(", "));
}
}
const pizza = new Food("피자");
pizza.addBrand("피자헛");
pizza.addBrand("도미노피자");
pizza.addBrand("파파존스");
pizza.print();
/*
피자 (를)을 파는 음식점들 :
피자헛, 도미노피자, 파파존스
*/
const chicken = new Food("치킨");
chicken.addBrand("굽네치킨");
chicken.addBrand("BBQ");
chicken.addBrand("bhc");
chicken.print();
/*
치킨 (를)을 파는 음식점들 :
굽네치킨, BBQ, bhc
*/
728x90
반응형
'Backend > Typescript & Angular' 카테고리의 다른 글
Truthy와 Falsy (0) | 2021.09.26 |
---|---|
삼항연산자 (0) | 2021.09.26 |
배열 내장함수 reduce (0) | 2021.09.06 |
shift/unshift & push/pop & concat & join (0) | 2021.08.28 |
splice & slice (0) | 2021.08.27 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- RDBMS
- git
- nestjs
- 바이트디그리
- MySQL
- 스마트폰중독
- 디지털디톡스
- TypeScript
- nestjs jest
- IT용어
- 클린아키텍쳐
- TDD
- 한달어스
- 습관개선
- Mongoose
- 개발용어
- OS
- 한달독서
- 참고
- Jekyll
- 갓생살자
- jest
- MongoDB
- Nest.js
- 미완
- vscode
- 나도 할 수 있다
- node.js
- gem
- typeORM
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
글 보관함