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

Using JavaScript objects in static() method?

Actually, when we try to usein static methodsUsingObject when, the result will be futile. But when the object is sent as a parameter, we can access the object. Let's briefly discuss this.

Example1

In the following example, we try to use the object " ""Parametersend, therefore, there is no result. If you openBrowser Console, we will receive an error " myComp.comp() is not a function"To get the actual resultParameter send, asExample2as shown.

<html>
<body>
<p id="method"></p>
<script>
   class Company {
      constructor(branch) {
         this.name = branch;
      }
      static comp() {
         return "Tutorix is the best e"-learning platform"
      }
   }
   myComp = new Company("Tesla");
   document.getElementById("method").innerHTML = myComp.comp();
</script>
</body>
</html>

Example2

In the following example,Object AsParameterTherefore, we will get the output as shown.

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

Output Result

Elon Musk is the head of Tesla