2

I have this piece of code

let person=new Object({
                       fname: 'Uday',
                       sname: 'Kiran',
                       getFName: function(){
                       return this.fname;
                      }
                     });

Object.defineProperty(person,'getSName',
                      {
                          value: function(){return this.sname;},
                          writable: false,
                          enumerable: true,
                          configurable: true
                      });

let personInherited=Object.create(person);

This is possible, I'm able to override getSName property with the help of Object.defineProperty eventhough it is defined as writable: false in person.

Object.defineProperty(personInherited,'getSName',
                      {
                         value: function(){
                                            return this.sname.toUpperCase();
                      },
                      writable: true,
                      enumerable: true,
                      configurable: true
                      });

But I'm not able override the getSName property like below.

personInherited.getSName=function(){
                                      return this.sname;
                                   }

Can someone please explain what is the difference between creating a property by using Object.defineProperty and directly assigning property using personInherited.getSName

Abhishek Anand
  • 447
  • 5
  • 22
Udaykiran R
  • 362
  • 2
  • 6
  • 16
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty there is all information you need to understand the diffrence – mtizziani Mar 29 '17 at 14:25
  • @mtizziani That doesn't explain how assignment and `defineProperty` differ for inherited properties that have `writable: false` (and/or `configurable: false`?), does it? That's what this question is asking about. – apsillers Mar 29 '17 at 14:30
  • 1
    [The documentation for `Object.defineProperty`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) is very clear; `writable: false` means the Property can't be re-assigned. Using `.defineProperty` on a Property that already exists, you are **modifying** the Property, rather than **assigning** it. The only way to prevent the usage of `.defineProperty` is to set the Property to `configurable : false`; However, this still allows the `writable` attribute to be changed. – Claies Mar 29 '17 at 14:42
  • @Claies Note that in the OP's example, setting `configurable: false` on the inherited property does not prevent setting a new instance property with `defineProperty`. I really don't think this is a situation handled well by MDN's documentation, because it fails to explain the difference in behavior for inherited properties versus own properties. In this case, `personInherited` doesn't have its own `getSName` property (it is inherited) and the only way to give it its own `getSName` property is by `defineProperty` since `writable: false` is specified to prevent the creation of an own-property. – apsillers Mar 29 '17 at 14:55

0 Answers0