HTML & CSS & JS/JS

[JS] 개체 생성자와 상속

가자미 2021. 3. 17. 16:54

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;