function Person(name) {
this.name = name;
}
Person.prototype = {
yell: function() {
console.log("my name is " + this.name);
}
}
/* 수정불가 */
var user = Object.create(Person.prototype, {
name: {
value: 'new name'
}
})
/* 수정가능 */
user = Object.create(Person.prototype, {
name : {
value: 'new name2',
configurable: true,
enumerable: true,
writable: true
}
})
/* 접근자 활용의 예 */
Object.defineProperties(user, {
firstName: {
value: "Younghwan",
writable: true
},
lastName: {
value: "Yang",
writable: true
},
fullName: {
get: function() {
return this.firstName + " " + this.lastName;
},
set: function(value) {
var res = value.split(' ');
if (res.length > 1) {
this.firstName = res[0];
this.lastName = res[1];
} else {
console.log('wrong format');
}
}
});
console.log(user.fullName);
user.fullName = "Hello world!";
console.log(user.firstName); // Hello
console.log(user.lastName); // World!
2018년 10월 29일 월요일
[javascript] 접근자 defineProperties를 이용한 생성자 설정.
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기