개, 고양이처럼 type, name, sound (종류, 이름, 짖는 소리)에 따라 만들고 싶다면?
function Animal(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
this.say = function() {
console.log(this.sound);
};
};
const dog1 = new Animal("개", "멍멍이", "멍멍");
dog1.say();
const dog2 = new Animal("개", "시바견", "머엉머엉");
dog2.say();
공통으로 사용되는 say를 일반화 하고 싶다면?
function Animal(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
}
Animal.prototype.say = function() {
console.log(this.sound);
};
Dog 클래스를 만들려면?
function Dog(name, sound) {
Animal.call(this, "개", name, sound);
}
Dog.prototype = Animal.prototype;
'웹(web) > 프론트엔드-javascript' 카테고리의 다른 글
falsy 한 값 (0) | 2020.06.28 |
---|---|
클래스 (0) | 2020.06.27 |
객체, 배열의 내장함수 정리 (0) | 2020.06.27 |
javascript 추가 기능 (0) | 2020.06.27 |
[Closure] (0) | 2020.06.03 |