Onclick event -

<button id="button-send" onclick="SendToOrderPhp.send()" class="button-on-form">Send</button> 

I want to pass to a function inside the class. And then the question is a static function - if I try to call a function, the class constructor will not do the work?

How to be in this case? Thank.

The code itself:

 class SendToOrderPhp{ private let name_; private let email_; constructor(let name, let email){ this.name=name; this.email=email; } static function send(){ fetch('/php/order.php',{ method: 'post', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({ name: name_, email: email_ }) }); } } SendToOrderPhp(GetInputsFromHtml.getName, GetInputsFromHtml.getEmail); SendToOrderPhp.send(); 
  • one
    Class static methods cannot directly use class fields. "How to be?" - transfer to a static method either an instance of the class, or the name and email values ​​themselves. - Regent
  • And about the transfer of the instance - this is the way out! Thank you very much! Write as an answer what I can mark) - HegoJune
  • On health. The answer added. - Regent

1 answer 1

Class static methods cannot directly use class fields.

"How to be?" :

  • you can pass an instance of a class to a static method
  • you can transfer to it the name and email values
  • can make the method non-static

In the first and third cases, an instance of the class must be stored somewhere, in the second, the values ​​themselves.