0
   class Something {
    a() {

    }
    get b() {
        return "bValue"
    }
}
    Something.prototype.eef = "dfd";
    window.someValue = "parent value";
   /*
     
     */
    Object.defineProperty(Something.prototype,"abc",{
    enumerable:true,
    configurable:true,
    get:function(){
        debugger;
        return "parent value";
    },
    set : function(value){
        debugger;
        window.someValue=value;
    }
    });

    class Derived extends Something {
  
    get b() {
        return "inside child bValue"
    }
}
class Derived1 extends Something{
};

/*
    obj1=new Derived1();
    obj1.abc.   should be "parent value" 
     */


case 1: setting property abc in Derived class

       Derived.prototype.abc="child value"; 
     /* obj2=new Derived();
        obj2.abc    should be "child value" but getting "parent 
       value"
  */

case 2: setting property abc in Derived class using Object.defineProperty

   Object.defineProperty(Derived.prototype,"abc",{
   value:"child value",
   enumerable:true,
   configurable:true
   });

/* obj3=new Derived();
       obj3.abc    output is "child value" which is expected
      
 */


I am defining property on Derived class directly using the above two method. I am expecting that when I create an object (obj=new Derived()) for both the cases and try to get abc property of class. Then I should get the same value ie. "child value" . But for my implementation I am somehow triggering the getter and setter defined on "abc" in parent class "Something" prototype which is not normal and because of this I am getting two different value for the two cases.The thing which I am not getting is how I can make changes to the prototype of parent through child.

jay102
  • 1
  • 1
  • Can you edit your question and add two snippets to illustrate the difference between the two behaviors? – Lajos Arpad Jul 26 '23 at 09:37
  • "*because of this I am getting two different value for the two cases.*" - how so? Even if you are triggering the setter, it should store the `"child value"` in the global `window.someValue` and then return that same value still when accessing `obj.abc`. – Bergi Jul 26 '23 at 09:43
  • edited my code to explain better. – jay102 Jul 26 '23 at 11:10
  • "*I am not able to understand how we are able affect parent.prototype by changing something in child.prototype*" - does my answer address this adequately? – Bergi Jul 26 '23 at 12:15
  • I changed the getter and setter a little bit to explain it clearly. You can check the getter it should return "parent value" when instance of Something class is used to invoke abc property.Then I am adding abc property in Derived class prototype ,so instance of Derived class should invoke abc defined in its own class prototype.But is not the case when I am setting property through Derived.prototype.It is working only when setting property through Object.defineProperty. – jay102 Jul 26 '23 at 13:10
  • https://stackoverflow.com/questions/26296497/how-to-override-a-defined-get-property-on-a-prototype, https://stackoverflow.com/questions/29191499/how-to-prevent-the-execution-of-getter-setter-methods-of-inherited-properties-in – Bergi Jul 26 '23 at 13:37
  • "*It is working only when setting property through `Object.defineProperty`*" - yes, that is expected. When you are using assignment syntax, you are *not* adding a property to the prototype object, you're just triggering the setter. So what exactly is your question? – Bergi Jul 26 '23 at 13:39
  • Yeah thats my question ,why Object.prototype is not adding property, but Object.defineProperty is able to do it. Is there a difference between how they add property to prototype. – jay102 Jul 27 '23 at 05:56
  • The difference is not `Derived.prototype`, which is the same object in both operations. It's `=` vs `Object.defineProperty`. And yes, they do behave differently! – Bergi Jul 27 '23 at 08:56

1 Answers1

0

I am somehow triggering the getter and setter defined on "abc" in parent class "Something" which is not normal

It is the normal behaviour of assignment. You are assigning the .abc property on an object which inherits from Something.prototype on which the setter is defined. It doesn't make a difference whether the assignment target is a new Something instance or the Derived.prototype object for that - they both have the same prototype chain.

If you want to override a property with a setter/getter in a subclass, you have to use Object.defineProperty - or just define the override in the class declaration.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375