41 lines
881 B
JavaScript
41 lines
881 B
JavaScript
function Person(age, name) {
|
|
this.name = name;
|
|
this.age = age;
|
|
};
|
|
|
|
Object.defineProperty(Person.prototype, "age", {
|
|
get: function() {
|
|
return this._age;
|
|
},
|
|
|
|
// Added a few things to demonstrate additional logic on the setter
|
|
set: function(num) {
|
|
num = parseInt(num, 10);
|
|
if(num > 0) {
|
|
this._age = num;
|
|
}
|
|
}
|
|
});
|
|
|
|
console.log(Person.prototype.hasOwnProperty('age'))
|
|
console.log(Person.hasOwnProperty('age'))
|
|
|
|
if(!(Person.prototype.hasOwnProperty('age'))){
|
|
Object.defineProperty(Person.prototype, "age", {
|
|
get: function() {
|
|
return this._age;
|
|
},
|
|
|
|
// Added a few things to demonstrate additional logic on the setter
|
|
set: function(num) {
|
|
num = parseInt(num, 10);
|
|
if(num > 0) {
|
|
this._age = num;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
a= new Person(1,2)
|
|
a.name=10
|
|
console.log(a.age) |