getOwnPropertyDescriptor returns undefine when it has a value

javascript, oop

Solution

The reason you're getting `undefined` is that the `employee` variable does not have an "own" property called `fullName`. That property belongs to `Person.prototype`.

Give this a try:

var fullName = Object.getOwnPropertyDescriptor(Person.prototype, "fullName");

If you change that line, the rest of your code should work as-is.

For reference (emphasis added):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor

The Object.getOwnPropertyDescriptor() method returns a property descriptor for an own property (that is, one directly present on an object, not present by dint of being along an object's prototype chain) of a given object.

Problem

I have been like one hour trying to solve with getOwnProperty returns undefined, I check my notes and cannot find the reason, could please some1 check or even better explain me why this behaviour??? Main goal in here is just overwrite a property on a method that extends another one. Here is the 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; /*over-w sayHi*/ var sayHifn = employee.sayHi.bind(employee); employee.sayHi = function() { return sayHifn() + " my name is " + this.firstName; }; /*over-w fullName*/ 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); ```

Original source