English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Statische Methoden in der JavaScript-Klasse?

Statische Methoden

verwendenstatische Methoden,können wir nur auf die Elemente der Klasse zugreifen,Statische Methoden. 

Beispiel1

In den folgenden Beispielen, static()Methoden in der Klasse " Company "startet nicht im Objekt "myComp",static() Der Inhalt im Methodenaufruf wird ausgeführt.

<html>
<body>
<p id="method"></p>
<script>
   class Company {
      constructor(branch) {
         this.name = branch;
      }
      static comp() {
         return "Tutorix ist die beste e-lernplattform"
      }
   }
   myComp = new Company("Tesla");
   document.getElementById("method").innerHTML = Company.comp();
</script>
</body>
</html>

Ausgabenergebnis

Tutorix ist die beste e-lernplattform


Beispiel2

In den folgenden Beispielen wird ein Objekt aufgerufen,class, daher wird nichts ausgegeben. Wenn Sie den Browserkonsole öffnen, myComp.comp() "ist keine Funktion."

<html>
<body>
<p id="method"></p>
<script>
   class Company {
      constructor(branch) {
         this.name = branch;
      }
      static comp() {
         return "Tutorix ist die beste e-lernplattform"
      }
   }
   myComp = new Company("Tesla");
   document.getElementById("method").innerHTML = myComp.comp();
</script>
</body>
</html>