Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  171] [ 1]  / answers: 1 / hits: 23279  / 11 Years ago, thu, january 30, 2014, 12:00:00

I basically having hard times understanding why I cannot overwrite an object property when using inheritance from another object, it goes like this.



var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};

Object.defineProperties(Person.prototype, {
sayHi : {
value :function() {
return Hi there;
},
enumerable : true,
writable : true
},
fullName : {
get : function() {
return this.firstName + + this.lastName;
},
enumerable : true
}
});


var createEmployee = function(firstName, lastName, ocupation) {
var employee = new Person(firstName, lastName);
employee.ocupation = ocupation;

Object.defineProperty(employee, sayHi, {
sayHi : {
value : function() {
return Hey I am + this.firstName;
}
}
});

return employee;
};

var janeDoe = createEmployee('Jane', 'Doe', 'Actress');


So, accoding to what I understand, I should overwrite the sayHi property on the employee object, however, I get it as undefined.



Could please some1 show me money?



here is the jsfiddle



Best Regards.


More From » javascript

 Answers
12

Just realized the answer I gave was bad.



You don't need the name inside the object there.



Object.defineProperty(employee, sayHi, {
sayHi : {//<- don't need this.
value : function() {
return Hey I am + this.firstName;
}
}//<- or this.
});

[#72853] Tuesday, January 28, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
charisma

Total Points: 1
Total Questions: 99
Total Answers: 117

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago
;