Class Methods
Methods are functions that describe the class in some way. Real Students can do actions like studying, sleeping, talking, etc, so we can define it as methods of the class:
123456789101112class Student{ name; university; age; gpa; sleep(){ console.log('I am sleeping right now!'); } } s = new Student(); s.sleep();
We added method sleep()
, which prints a message.
This method is trivial, but in general, methods can also be used for:
- Changing some attribute’s value
- Extracting some attribute’s value
- Calling other methods inside
- Printing something to the console etc.
Let’s demonstrate it and add the method happyBirthday()
. This method increases age of the student and prints some info about it:
12345678910111213141516class Student{ name; university; age; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday!'); } } s = new Student(); s.happyBirthday();
You can store some function arguments.
12345// method of the class happyBirthday(newAge){ this.age = newAge; console.log('Today is my birthday!'); }
Swipe to start coding
Modify sleep()
method: store argument hours
in it and output the message: 'I am going to sleep for '+hours+' hours'
instead of the current message inside this method.
Lösning
Actually you already met attributes and methods in other objects. For example,
length
is a attribute of some object of the classarray
. In theconsole.log()
,console
is a class to write some data to the stream, andlog()
is a method, that prints your data (like 'hello world') to the stream.
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
Class Methods
Methods are functions that describe the class in some way. Real Students can do actions like studying, sleeping, talking, etc, so we can define it as methods of the class:
123456789101112class Student{ name; university; age; gpa; sleep(){ console.log('I am sleeping right now!'); } } s = new Student(); s.sleep();
We added method sleep()
, which prints a message.
This method is trivial, but in general, methods can also be used for:
- Changing some attribute’s value
- Extracting some attribute’s value
- Calling other methods inside
- Printing something to the console etc.
Let’s demonstrate it and add the method happyBirthday()
. This method increases age of the student and prints some info about it:
12345678910111213141516class Student{ name; university; age; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday!'); } } s = new Student(); s.happyBirthday();
You can store some function arguments.
12345// method of the class happyBirthday(newAge){ this.age = newAge; console.log('Today is my birthday!'); }
Swipe to start coding
Modify sleep()
method: store argument hours
in it and output the message: 'I am going to sleep for '+hours+' hours'
instead of the current message inside this method.
Lösning
Actually you already met attributes and methods in other objects. For example,
length
is a attribute of some object of the classarray
. In theconsole.log()
,console
is a class to write some data to the stream, andlog()
is a method, that prints your data (like 'hello world') to the stream.
Tack för dina kommentarer!
single
Awesome!
Completion rate improved to 7.69
Class Methods
Svep för att visa menyn
Methods are functions that describe the class in some way. Real Students can do actions like studying, sleeping, talking, etc, so we can define it as methods of the class:
123456789101112class Student{ name; university; age; gpa; sleep(){ console.log('I am sleeping right now!'); } } s = new Student(); s.sleep();
We added method sleep()
, which prints a message.
This method is trivial, but in general, methods can also be used for:
- Changing some attribute’s value
- Extracting some attribute’s value
- Calling other methods inside
- Printing something to the console etc.
Let’s demonstrate it and add the method happyBirthday()
. This method increases age of the student and prints some info about it:
12345678910111213141516class Student{ name; university; age; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday!'); } } s = new Student(); s.happyBirthday();
You can store some function arguments.
12345// method of the class happyBirthday(newAge){ this.age = newAge; console.log('Today is my birthday!'); }
Swipe to start coding
Modify sleep()
method: store argument hours
in it and output the message: 'I am going to sleep for '+hours+' hours'
instead of the current message inside this method.
Lösning
Actually you already met attributes and methods in other objects. For example,
length
is a attribute of some object of the classarray
. In theconsole.log()
,console
is a class to write some data to the stream, andlog()
is a method, that prints your data (like 'hello world') to the stream.
Tack för dina kommentarer!