I tried to solve getOwnProperty undefined for one hour, I check my notes and cannot find the reason, I might like to check some1 or even better explain to me why this behavior ??? The main goal here is simply to rewrite the property to a method that extends another one.
Here is jsfiddle
var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
Object.defineProperties(Person.prototype, {
sayHi : {
value : function() {
return "Hi there";
},
writable: true,
enumerable : true
},
fullName : {
get : function() {
return this.firstName + " " + this.lastName;
},
configurable : true,
enumerable : true
}
});
var createEmployee = function(firstName, lastName, ocupation) {
var employee = new Person(firstName, lastName);
employee.ocupation = ocupation;
var sayHifn = employee.sayHi.bind(employee);
employee.sayHi = function() {
return sayHifn() + " my name is " + this.firstName;
};
var fullName = Object.getOwnPropertyDescriptor(employee, "fullName");
var fullNameFn = fullName.get.bind(employee);
Object.defineProperty(employee, 'fullName', {
get : function() {
return fullNameFn() + " this is o-w ";
}
});
return employee;
};
var record = createEmployee('jhon', 'doe', 'eng');
console.log(record);
source
share