Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Static Functions | Static
Object-Oriented Programming in JavaScript
course content

Kursinhalt

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

1. Classes & Objects
2. Encapsulation
3. Static
4. Inheritance

book
Static Functions

Methods also can be static. Let’s create compare() method with two parameters: studentA and studentB. This function will output which student has a higher GPA.

123456789101112131415161718192021222324
class Student{ name; age; static ageOfMajority = 18; university; gpa; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } static compare(studentA, studentB) { if (studentA.gpa > studentB.gpa) { console.log('First student has higher GPA.') } else if (studentA.gpa < studentB.gpa) { console.log('Second student has higher GPA.') } else { console.log('GPAs are equal.') } } } rob = new Student('Robert', 22); rob.gpa = 3.4; steve = new Student('Steve', 19); steve.gpa = 2.9 Student.compare(rob, steve);
copy

To call this method, you also have to use the name of the class, not the object:

1
Student.compare(studentA, studentB);
copy

Inside the static method, you can refer only to static class attributes, using this keyword, because non-static attributes are unique for each object, so for the whole class, they are undefined.

Note that in function compare() you refer to the non-static objects attributes studentA and studentB, but to the static class attributes Student.

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
toggle bottom row

book
Static Functions

Methods also can be static. Let’s create compare() method with two parameters: studentA and studentB. This function will output which student has a higher GPA.

123456789101112131415161718192021222324
class Student{ name; age; static ageOfMajority = 18; university; gpa; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } static compare(studentA, studentB) { if (studentA.gpa > studentB.gpa) { console.log('First student has higher GPA.') } else if (studentA.gpa < studentB.gpa) { console.log('Second student has higher GPA.') } else { console.log('GPAs are equal.') } } } rob = new Student('Robert', 22); rob.gpa = 3.4; steve = new Student('Steve', 19); steve.gpa = 2.9 Student.compare(rob, steve);
copy

To call this method, you also have to use the name of the class, not the object:

1
Student.compare(studentA, studentB);
copy

Inside the static method, you can refer only to static class attributes, using this keyword, because non-static attributes are unique for each object, so for the whole class, they are undefined.

Note that in function compare() you refer to the non-static objects attributes studentA and studentB, but to the static class attributes Student.

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
We're sorry to hear that something went wrong. What happened?
some-alt