프로토타입 (Prototype)
프로토타입 패턴
프토토타입 동작
Person.prototype.isPrototypeOf(person1); // true
Person.prototype.isPrototypeOf(person2); // true동적 성질
상속
Last updated
Person.prototype.isPrototypeOf(person1); // true
Person.prototype.isPrototypeOf(person2); // trueLast updated
function Person (name){
this.name = name;
}
var juno = new Person('Junho Park');
juno.sayName(); // error
Person.prototype.sayName = function(){
console.log(this.name);
}
juno.sayName(); // 'Junho Park' 출력function Person (name){
this.name = name;
}
Person.prototype.aboutMe = function(){
console.log(this.name);
}
function Man (name) {
Person.call(this, name);
this.gender = 'MALE';
}
// Man은 Person을 상속받는다.
Man.prototype = new Person();
// 상속받은 메서드 재정의
Man.prototype.aboutMe = function(){
console.log(this.gender, this.name);
}
console.log(Man.prototype.constructor === Person); // true