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

Come usare un oggetto JavaScript nel metodo static()?

实际上,当我们尝试在静态方法中使用oggetto 时,结果将是徒劳的。但是当对象作为参数发送时,我们可以访问该对象。让我们简要地讨论一下。

示例1

在下面的示例中,我们尝试直接使用对象“ myComp ”,而不是将其作为Parametro发送,因此,没有任何结果。如果打开浏览器控制台 Parametro Mandare, comeEsempio 2come indicato.

<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>

Esempio 2

Nell'esempio seguente,oggetto comeParametroMandare. Pertanto otterremo come indicato nell'output.

<html>
<body>
<p id="method"></p>
<script>
   class Company {
      constructor(branch) {
         this.name = branch;
      }
      static comp(val) {
         return "Elon Musk è il capo di " + val.name
      }
   }
   myComp = new Company("Tesla");
   document.getElementById("method").innerHTML = Company.comp(myComp);
</script>
</body>
</html>

Risultato di output

Elon Musk è il capo di Tesla