Is GET or SET defined in class












2















I try to find out, if the current derived class has a property setter. Reason is that I loop a couple of options through the constructors of extended classes. and just want to assign those properties, that are part of this class and not the base class.



So I need a possibility to find out, if the getter or setter exist for this class or not. I remove as much code as possible to show case the issue only.



This is the base class:



class baseClass{
constructor(wOptions){
//do some stuff
if(typeof wOptions == "object"){
for(let prop in wOptions){
// if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
// if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
this[prop] = wOptions[prop];
delete wOptions[prop];
}
}
}
}
get mainProperty(){
return 42;
}
set mainProperty(newValue){
return 42;
}
}


Here is the the derived class:



class derivatedClass extends baseClass{
constructor(wOptions){
super(wOptions)
//do some other stuff
let html = document.body;
Object.defineProperty(this, "html", {value: html, writable: false});

if(typeof wOptions == "object"){
for(let prop in wOptions){
// if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
// if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
this[prop] = wOptions[prop];
delete wOptions[prop];
}
}
}
}
get otherProperty(){
return html.innerText;
}
set otherProperty(newValue){
return html.innerText = newValue;
}
}


And how to initialize the object:



let options = {otherProperty: "new Text"};
let object = new derivatedClass(options);
console.log(options);


Regarding the solutions tried already:





  • getOwnPropertyDescriptor always returns undefined; returns the options as assigned


  • hasOwnProperty always returns false ; returns the options as assigned


  • typeof this[prop] != "undefined" calls the getter and this can be pretty bad, because html is not defined yet. Reference Error for html.innerText

  • Not a solution, but for verification: Removing the if clause in the derived class, changes the body text to new Text and prints an empty object in the console.


Tested in Chrome 71.



There would be some options to avoid this:




  • shift all properties handled by this class to another object (pretty ugly and high possibility to forget a property in the list)

  • use always Object.defineProperty, because they will be executed after the super call. However, it is not that pretty than the get/set functions of the class construct.


Is there any possibility to find out if there is a setter available for otherProperty, that evaluates true in the derivatedClass but not in the baseClass?










share|improve this question





























    2















    I try to find out, if the current derived class has a property setter. Reason is that I loop a couple of options through the constructors of extended classes. and just want to assign those properties, that are part of this class and not the base class.



    So I need a possibility to find out, if the getter or setter exist for this class or not. I remove as much code as possible to show case the issue only.



    This is the base class:



    class baseClass{
    constructor(wOptions){
    //do some stuff
    if(typeof wOptions == "object"){
    for(let prop in wOptions){
    // if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
    // if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
    if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
    this[prop] = wOptions[prop];
    delete wOptions[prop];
    }
    }
    }
    }
    get mainProperty(){
    return 42;
    }
    set mainProperty(newValue){
    return 42;
    }
    }


    Here is the the derived class:



    class derivatedClass extends baseClass{
    constructor(wOptions){
    super(wOptions)
    //do some other stuff
    let html = document.body;
    Object.defineProperty(this, "html", {value: html, writable: false});

    if(typeof wOptions == "object"){
    for(let prop in wOptions){
    // if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
    // if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
    if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
    this[prop] = wOptions[prop];
    delete wOptions[prop];
    }
    }
    }
    }
    get otherProperty(){
    return html.innerText;
    }
    set otherProperty(newValue){
    return html.innerText = newValue;
    }
    }


    And how to initialize the object:



    let options = {otherProperty: "new Text"};
    let object = new derivatedClass(options);
    console.log(options);


    Regarding the solutions tried already:





    • getOwnPropertyDescriptor always returns undefined; returns the options as assigned


    • hasOwnProperty always returns false ; returns the options as assigned


    • typeof this[prop] != "undefined" calls the getter and this can be pretty bad, because html is not defined yet. Reference Error for html.innerText

    • Not a solution, but for verification: Removing the if clause in the derived class, changes the body text to new Text and prints an empty object in the console.


    Tested in Chrome 71.



    There would be some options to avoid this:




    • shift all properties handled by this class to another object (pretty ugly and high possibility to forget a property in the list)

    • use always Object.defineProperty, because they will be executed after the super call. However, it is not that pretty than the get/set functions of the class construct.


    Is there any possibility to find out if there is a setter available for otherProperty, that evaluates true in the derivatedClass but not in the baseClass?










    share|improve this question



























      2












      2








      2








      I try to find out, if the current derived class has a property setter. Reason is that I loop a couple of options through the constructors of extended classes. and just want to assign those properties, that are part of this class and not the base class.



      So I need a possibility to find out, if the getter or setter exist for this class or not. I remove as much code as possible to show case the issue only.



      This is the base class:



      class baseClass{
      constructor(wOptions){
      //do some stuff
      if(typeof wOptions == "object"){
      for(let prop in wOptions){
      // if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
      // if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
      if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
      this[prop] = wOptions[prop];
      delete wOptions[prop];
      }
      }
      }
      }
      get mainProperty(){
      return 42;
      }
      set mainProperty(newValue){
      return 42;
      }
      }


      Here is the the derived class:



      class derivatedClass extends baseClass{
      constructor(wOptions){
      super(wOptions)
      //do some other stuff
      let html = document.body;
      Object.defineProperty(this, "html", {value: html, writable: false});

      if(typeof wOptions == "object"){
      for(let prop in wOptions){
      // if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
      // if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
      if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
      this[prop] = wOptions[prop];
      delete wOptions[prop];
      }
      }
      }
      }
      get otherProperty(){
      return html.innerText;
      }
      set otherProperty(newValue){
      return html.innerText = newValue;
      }
      }


      And how to initialize the object:



      let options = {otherProperty: "new Text"};
      let object = new derivatedClass(options);
      console.log(options);


      Regarding the solutions tried already:





      • getOwnPropertyDescriptor always returns undefined; returns the options as assigned


      • hasOwnProperty always returns false ; returns the options as assigned


      • typeof this[prop] != "undefined" calls the getter and this can be pretty bad, because html is not defined yet. Reference Error for html.innerText

      • Not a solution, but for verification: Removing the if clause in the derived class, changes the body text to new Text and prints an empty object in the console.


      Tested in Chrome 71.



      There would be some options to avoid this:




      • shift all properties handled by this class to another object (pretty ugly and high possibility to forget a property in the list)

      • use always Object.defineProperty, because they will be executed after the super call. However, it is not that pretty than the get/set functions of the class construct.


      Is there any possibility to find out if there is a setter available for otherProperty, that evaluates true in the derivatedClass but not in the baseClass?










      share|improve this question
















      I try to find out, if the current derived class has a property setter. Reason is that I loop a couple of options through the constructors of extended classes. and just want to assign those properties, that are part of this class and not the base class.



      So I need a possibility to find out, if the getter or setter exist for this class or not. I remove as much code as possible to show case the issue only.



      This is the base class:



      class baseClass{
      constructor(wOptions){
      //do some stuff
      if(typeof wOptions == "object"){
      for(let prop in wOptions){
      // if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
      // if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
      if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
      this[prop] = wOptions[prop];
      delete wOptions[prop];
      }
      }
      }
      }
      get mainProperty(){
      return 42;
      }
      set mainProperty(newValue){
      return 42;
      }
      }


      Here is the the derived class:



      class derivatedClass extends baseClass{
      constructor(wOptions){
      super(wOptions)
      //do some other stuff
      let html = document.body;
      Object.defineProperty(this, "html", {value: html, writable: false});

      if(typeof wOptions == "object"){
      for(let prop in wOptions){
      // if(Object.getOwnPropertyDescriptor(this, prop) != undefined){
      // if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */
      if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */
      this[prop] = wOptions[prop];
      delete wOptions[prop];
      }
      }
      }
      }
      get otherProperty(){
      return html.innerText;
      }
      set otherProperty(newValue){
      return html.innerText = newValue;
      }
      }


      And how to initialize the object:



      let options = {otherProperty: "new Text"};
      let object = new derivatedClass(options);
      console.log(options);


      Regarding the solutions tried already:





      • getOwnPropertyDescriptor always returns undefined; returns the options as assigned


      • hasOwnProperty always returns false ; returns the options as assigned


      • typeof this[prop] != "undefined" calls the getter and this can be pretty bad, because html is not defined yet. Reference Error for html.innerText

      • Not a solution, but for verification: Removing the if clause in the derived class, changes the body text to new Text and prints an empty object in the console.


      Tested in Chrome 71.



      There would be some options to avoid this:




      • shift all properties handled by this class to another object (pretty ugly and high possibility to forget a property in the list)

      • use always Object.defineProperty, because they will be executed after the super call. However, it is not that pretty than the get/set functions of the class construct.


      Is there any possibility to find out if there is a setter available for otherProperty, that evaluates true in the derivatedClass but not in the baseClass?







      javascript class properties getter-setter exists






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 22:20









      Uwe Keim

      27.6k32132212




      27.6k32132212










      asked Jan 1 at 21:42









      FrankFrank

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Try this one



          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, 'otherProperty');
          console.log(descriptor);


          If derivatedClass.prototype has an otherProperty (otherwise returns undefined), it will return an object that contains some values. In the returned object, you should see get and set



          More info here






          share|improve this answer


























          • const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

            – Frank
            Jan 1 at 22:44













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999173%2fis-get-or-set-defined-in-class%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Try this one



          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, 'otherProperty');
          console.log(descriptor);


          If derivatedClass.prototype has an otherProperty (otherwise returns undefined), it will return an object that contains some values. In the returned object, you should see get and set



          More info here






          share|improve this answer


























          • const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

            – Frank
            Jan 1 at 22:44


















          1














          Try this one



          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, 'otherProperty');
          console.log(descriptor);


          If derivatedClass.prototype has an otherProperty (otherwise returns undefined), it will return an object that contains some values. In the returned object, you should see get and set



          More info here






          share|improve this answer


























          • const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

            – Frank
            Jan 1 at 22:44
















          1












          1








          1







          Try this one



          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, 'otherProperty');
          console.log(descriptor);


          If derivatedClass.prototype has an otherProperty (otherwise returns undefined), it will return an object that contains some values. In the returned object, you should see get and set



          More info here






          share|improve this answer















          Try this one



          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, 'otherProperty');
          console.log(descriptor);


          If derivatedClass.prototype has an otherProperty (otherwise returns undefined), it will return an object that contains some values. In the returned object, you should see get and set



          More info here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 22:20









          Uwe Keim

          27.6k32132212




          27.6k32132212










          answered Jan 1 at 22:18









          user3790180user3790180

          10819




          10819













          • const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

            – Frank
            Jan 1 at 22:44





















          • const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

            – Frank
            Jan 1 at 22:44



















          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

          – Frank
          Jan 1 at 22:44







          const descriptor = Object.getOwnPropertyDescriptor(derivatedClass.prototype, prop) if(descriptor !== undefined && typeof descriptor.set == "function") works for me!

          – Frank
          Jan 1 at 22:44






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999173%2fis-get-or-set-defined-in-class%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas

          Can't read property showImagePicker of undefined in react native iOS