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.
123456789101112131415161718192021222324class 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);
To call this method, you also have to use the name of the class, not the object:
1Student.compare(studentA, studentB);
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
.
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 7.69Awesome!
Completion rate improved to 7.69
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.
123456789101112131415161718192021222324class 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);
To call this method, you also have to use the name of the class, not the object:
1Student.compare(studentA, studentB);
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
.
Tack för dina kommentarer!
single
Awesome!
Completion rate improved to 7.69
Static Functions
Svep för att visa menyn
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.
123456789101112131415161718192021222324class 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);
To call this method, you also have to use the name of the class, not the object:
1Student.compare(studentA, studentB);
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
.
Tack för dina kommentarer!