Have a question?
Message sent Close
View Categories

Class Static

< 1 min read

Агуулга

Static class methods are defined on the class itself.

You cannot call a static method on an object, only on an object class.

 

Example #

class Car { constructor(name) { this.name = name; } static hello() { return "Hello!!"; } } let myCar = new Car("Ford"); // You can calll 'hello()' on the Car Class: document.getElementById("demo").innerHTML = Car.hello(); // But NOT on a Car Object: // document.getElementById("demo").innerHTML = myCar.hello(); // this will raise an error.

If you want to use the myCar object inside the static method, you can send it as a parameter:

Example #

class Car { constructor(name) { this.name = name; } static hello(x) { return "Hello " + x.name; } } let myCar = new Car("Ford"); document.getElementById("demo").innerHTML = Car.hello(myCar);

Powered by BetterDocs

Leave a Reply