English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
usandoMetodo statico,Possiamo accedere solo agli elementi della classe, non agli elementi dell'oggetto. Può essere chiamato solo all'interno della classe, non nell'oggetto.Metodo statico。
Nell'esempio seguente, static()Il metodo nella classe “ Company ”invece che nell'oggetto “myComp”. Quindi,static() Il contenuto del metodo viene eseguito nell'output.
<html> <body> <p id="method"></p> <script> class Company { constructor(branch) { this.name = branch; } static comp() { return "Tutorix è la migliore piattaforma di e-learning" } } myComp = new Company("Tesla"); document.getElementById("method").innerHTML = Company.comp(); </script> </body> </html>
Tutorix è la migliore piattaforma di e-learning
Nell'esempio seguente, verrà chiamato l'oggetto, nonclass,quindi non verrà eseguito alcun output. Se si apre la console del browser, verrà visualizzato un errore, indicando che “ myComp.comp() ”non è una funzione.
<html> <body> <p id="method"></p> <script> class Company { constructor(branch) { this.name = branch; } static comp() { return "Tutorix è la migliore piattaforma di e-learning" } } myComp = new Company("Tesla"); document.getElementById("method").innerHTML = myComp.comp(); </script> </body> </html>