/* Object.create와 new 키워드 조합
Object.create과 new 키워드를 조합하여 생성자의 연결이 깨지지 않도록 해보자.
*/
function Person() {
this.name = "unknown";
}
function User() {
this.name = "user";
}
// 두 번째 매개변수는 키는 속성명으로
// 새로 만든 객체에 추가될 속성 설명자(descriptor)를 지정합니다.
// writable option defaults to false.
// https://stackoverflow.com/questions/7757337/defining-read-only-properties-in-javascript
User.prototype = Object.create(Person.prototype, {
constructor: { // Person.prototype.constructor 는 이제 읽기전용이다.
value: User // 왜냐하면 기본이 writable = false 이다.
}
})
var myuser = new User();
console.log(myuser instanceof User);
console.log(myuser instanceof Person);
console.log(myuser.constructor);
이 녀석은 이전
2018년 12월 11일 화요일
[javascript][prototype] Object.create과 new 키워드로 생성자의 연결이 깨지지 않도록 - 03
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기