Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind JavaScript Static Methods?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___(name) {3Order
Put the learning moves in the order that makes the concept easiest to apply.
You cannot call a static method on an object, only on an object class.
Static class methods are defined on the class itself.
JavaScript Static Methods
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!!";
}
}
const myCar = new Car("Ford");
// You can call '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;
}
}
const myCar = new Car("Ford");
document.getElementById("demo").innerHTML = Car.hello(myCar);