How to access the class itself from within a class without using its name [duplicate]












2















This question already has an answer here:




  • js call static method from class

    3 answers




Is it possible to access the class itself inside of a classes function:



class MyClass {
static get myFunction() { return "foo"; }

constructor() {
console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
console.log(this.myFunction); //undefined
console.log(this.prototype.myFunction); //Cannot read property 'myFunction' of undefined
}

}

new MyClass();


Is it possible to achieve the same as you do with MyClass.myFunction() and access the static methods without using the name of the class (in this case without using MyClass in this example?



Something like this.master.myFunction() (I’m just making master up here it’s obviously not called master)



JSBin: https://jsbin.com/hawituxosu/1/edit?js,console



Is that even possible? Thank you!










share|improve this question













marked as duplicate by Chris G, Jonas Wilms javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 11:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    2















    This question already has an answer here:




    • js call static method from class

      3 answers




    Is it possible to access the class itself inside of a classes function:



    class MyClass {
    static get myFunction() { return "foo"; }

    constructor() {
    console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
    console.log(this.myFunction); //undefined
    console.log(this.prototype.myFunction); //Cannot read property 'myFunction' of undefined
    }

    }

    new MyClass();


    Is it possible to achieve the same as you do with MyClass.myFunction() and access the static methods without using the name of the class (in this case without using MyClass in this example?



    Something like this.master.myFunction() (I’m just making master up here it’s obviously not called master)



    JSBin: https://jsbin.com/hawituxosu/1/edit?js,console



    Is that even possible? Thank you!










    share|improve this question













    marked as duplicate by Chris G, Jonas Wilms javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 21 '18 at 11:27


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      2












      2








      2








      This question already has an answer here:




      • js call static method from class

        3 answers




      Is it possible to access the class itself inside of a classes function:



      class MyClass {
      static get myFunction() { return "foo"; }

      constructor() {
      console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
      console.log(this.myFunction); //undefined
      console.log(this.prototype.myFunction); //Cannot read property 'myFunction' of undefined
      }

      }

      new MyClass();


      Is it possible to achieve the same as you do with MyClass.myFunction() and access the static methods without using the name of the class (in this case without using MyClass in this example?



      Something like this.master.myFunction() (I’m just making master up here it’s obviously not called master)



      JSBin: https://jsbin.com/hawituxosu/1/edit?js,console



      Is that even possible? Thank you!










      share|improve this question














      This question already has an answer here:




      • js call static method from class

        3 answers




      Is it possible to access the class itself inside of a classes function:



      class MyClass {
      static get myFunction() { return "foo"; }

      constructor() {
      console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
      console.log(this.myFunction); //undefined
      console.log(this.prototype.myFunction); //Cannot read property 'myFunction' of undefined
      }

      }

      new MyClass();


      Is it possible to achieve the same as you do with MyClass.myFunction() and access the static methods without using the name of the class (in this case without using MyClass in this example?



      Something like this.master.myFunction() (I’m just making master up here it’s obviously not called master)



      JSBin: https://jsbin.com/hawituxosu/1/edit?js,console



      Is that even possible? Thank you!





      This question already has an answer here:




      • js call static method from class

        3 answers








      javascript ecmascript-6 es6-class






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 11:19









      chitzui

      678719




      678719




      marked as duplicate by Chris G, Jonas Wilms javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 '18 at 11:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Chris G, Jonas Wilms javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 '18 at 11:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          4 Answers
          4






          active

          oldest

          votes


















          2














          you can use constructor for this




          Returns a reference to the Object constructor function that created the instance object




          The constructor property has three purposes:



          1. Get the class object.



          2. Create an new instance



          3. Invoke super constructor



          class MyClass {
          static get myFunction() { return "foo"; }

          constructor() {
          console.log(this.constructor.myFunction);
          }

          }

          new MyClass();





          share|improve this answer



















          • 1




            It’s exactly what I was looking for! That is fabulous! Thank you
            – chitzui
            Nov 21 '18 at 11:35



















          2














          an option you can do here is to call static method inside an instance method and call that instance method



          class MyClass {
          static get myFunction() { return "foo"; }

          constructor() {
          console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
          console.log(this.constructor.myFunction);
          }
          myFunction2() {
          return this.constructor.myFunction();
          }
          }
          const master = new MyClass();
          master.myFunction2();





          share|improve this answer





























            1














               this.constructor.myFoo


            The constructor property helps you here






            share|improve this answer































              1














              You could work around getting to the class name using:



              this.__proto__.constructor.name   // MyClass





              class MyClass {
              static get myFunction() { return "foo"; }

              constructor() {

              console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
              }

              }

              new MyClass();








              share|improve this answer




























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                you can use constructor for this




                Returns a reference to the Object constructor function that created the instance object




                The constructor property has three purposes:



                1. Get the class object.



                2. Create an new instance



                3. Invoke super constructor



                class MyClass {
                static get myFunction() { return "foo"; }

                constructor() {
                console.log(this.constructor.myFunction);
                }

                }

                new MyClass();





                share|improve this answer



















                • 1




                  It’s exactly what I was looking for! That is fabulous! Thank you
                  – chitzui
                  Nov 21 '18 at 11:35
















                2














                you can use constructor for this




                Returns a reference to the Object constructor function that created the instance object




                The constructor property has three purposes:



                1. Get the class object.



                2. Create an new instance



                3. Invoke super constructor



                class MyClass {
                static get myFunction() { return "foo"; }

                constructor() {
                console.log(this.constructor.myFunction);
                }

                }

                new MyClass();





                share|improve this answer



















                • 1




                  It’s exactly what I was looking for! That is fabulous! Thank you
                  – chitzui
                  Nov 21 '18 at 11:35














                2












                2








                2






                you can use constructor for this




                Returns a reference to the Object constructor function that created the instance object




                The constructor property has three purposes:



                1. Get the class object.



                2. Create an new instance



                3. Invoke super constructor



                class MyClass {
                static get myFunction() { return "foo"; }

                constructor() {
                console.log(this.constructor.myFunction);
                }

                }

                new MyClass();





                share|improve this answer














                you can use constructor for this




                Returns a reference to the Object constructor function that created the instance object




                The constructor property has three purposes:



                1. Get the class object.



                2. Create an new instance



                3. Invoke super constructor



                class MyClass {
                static get myFunction() { return "foo"; }

                constructor() {
                console.log(this.constructor.myFunction);
                }

                }

                new MyClass();






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 11:32

























                answered Nov 21 '18 at 11:27









                omri_saadon

                6,95031444




                6,95031444








                • 1




                  It’s exactly what I was looking for! That is fabulous! Thank you
                  – chitzui
                  Nov 21 '18 at 11:35














                • 1




                  It’s exactly what I was looking for! That is fabulous! Thank you
                  – chitzui
                  Nov 21 '18 at 11:35








                1




                1




                It’s exactly what I was looking for! That is fabulous! Thank you
                – chitzui
                Nov 21 '18 at 11:35




                It’s exactly what I was looking for! That is fabulous! Thank you
                – chitzui
                Nov 21 '18 at 11:35













                2














                an option you can do here is to call static method inside an instance method and call that instance method



                class MyClass {
                static get myFunction() { return "foo"; }

                constructor() {
                console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
                console.log(this.constructor.myFunction);
                }
                myFunction2() {
                return this.constructor.myFunction();
                }
                }
                const master = new MyClass();
                master.myFunction2();





                share|improve this answer


























                  2














                  an option you can do here is to call static method inside an instance method and call that instance method



                  class MyClass {
                  static get myFunction() { return "foo"; }

                  constructor() {
                  console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
                  console.log(this.constructor.myFunction);
                  }
                  myFunction2() {
                  return this.constructor.myFunction();
                  }
                  }
                  const master = new MyClass();
                  master.myFunction2();





                  share|improve this answer
























                    2












                    2








                    2






                    an option you can do here is to call static method inside an instance method and call that instance method



                    class MyClass {
                    static get myFunction() { return "foo"; }

                    constructor() {
                    console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
                    console.log(this.constructor.myFunction);
                    }
                    myFunction2() {
                    return this.constructor.myFunction();
                    }
                    }
                    const master = new MyClass();
                    master.myFunction2();





                    share|improve this answer












                    an option you can do here is to call static method inside an instance method and call that instance method



                    class MyClass {
                    static get myFunction() { return "foo"; }

                    constructor() {
                    console.log(MyClass.myFunction); // "foo" -- whith using its name it works. This is what we want but without using the name
                    console.log(this.constructor.myFunction);
                    }
                    myFunction2() {
                    return this.constructor.myFunction();
                    }
                    }
                    const master = new MyClass();
                    master.myFunction2();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 11:26









                    Artyom Amiryan

                    1,835113




                    1,835113























                        1














                           this.constructor.myFoo


                        The constructor property helps you here






                        share|improve this answer




























                          1














                             this.constructor.myFoo


                          The constructor property helps you here






                          share|improve this answer


























                            1












                            1








                            1






                               this.constructor.myFoo


                            The constructor property helps you here






                            share|improve this answer














                               this.constructor.myFoo


                            The constructor property helps you here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            answered Nov 21 '18 at 11:26


























                            community wiki





                            Jonas Wilms
























                                1














                                You could work around getting to the class name using:



                                this.__proto__.constructor.name   // MyClass





                                class MyClass {
                                static get myFunction() { return "foo"; }

                                constructor() {

                                console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
                                }

                                }

                                new MyClass();








                                share|improve this answer


























                                  1














                                  You could work around getting to the class name using:



                                  this.__proto__.constructor.name   // MyClass





                                  class MyClass {
                                  static get myFunction() { return "foo"; }

                                  constructor() {

                                  console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
                                  }

                                  }

                                  new MyClass();








                                  share|improve this answer
























                                    1












                                    1








                                    1






                                    You could work around getting to the class name using:



                                    this.__proto__.constructor.name   // MyClass





                                    class MyClass {
                                    static get myFunction() { return "foo"; }

                                    constructor() {

                                    console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
                                    }

                                    }

                                    new MyClass();








                                    share|improve this answer












                                    You could work around getting to the class name using:



                                    this.__proto__.constructor.name   // MyClass





                                    class MyClass {
                                    static get myFunction() { return "foo"; }

                                    constructor() {

                                    console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
                                    }

                                    }

                                    new MyClass();








                                    class MyClass {
                                    static get myFunction() { return "foo"; }

                                    constructor() {

                                    console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
                                    }

                                    }

                                    new MyClass();





                                    class MyClass {
                                    static get myFunction() { return "foo"; }

                                    constructor() {

                                    console.log(eval(this.__proto__.constructor.name + ".myFunction")); //foo
                                    }

                                    }

                                    new MyClass();






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 21 '18 at 11:31









                                    Ahmad

                                    8,20243463




                                    8,20243463















                                        Popular posts from this blog

                                        Wiesbaden

                                        Marschland

                                        Dieringhausen