Ayyuka Static Methods of JavaScript
- Previous page JS class inheritance
- Next page JS callback
Method static na shirin a kan shirin dake.
Anu kaiyada a kan shirin. static
方法,kana yaua a kan shirin dake.
instance
class Car { constructor(name) { this.name = name; } static hello() { return "Hello!!"; } } let myCar = new Car("Ford"); // You can call 'hello()' on the Car class: // document.getElementById("demo").innerHTML = Car.hello(); // But you cannot call on the Car object: // document.getElementById("demo").innerHTML = myCar.hello(); // This will cause an error.
If you want to static
In the method, you can use the myCar object and send it as a parameter:
instance
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);
- Previous page JS class inheritance
- Next page JS callback