Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Class Methods | Classes & Objects
Object-Oriented Programming in JavaScript
course content

Course Content

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

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

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:

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:

You can store some function arguments.

Task

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.

Task

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.

Actually you already met attributes and methods in other objects. For example, length is a attribute of some object of the class array. In the console.log(), console is a class to write some data to the stream, and log() is a method, that prints your data (like 'hello world') to the stream.

Everything was clear?

Section 1. Chapter 3
toggle bottom row

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:

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:

You can store some function arguments.

Task

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.

Task

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.

Actually you already met attributes and methods in other objects. For example, length is a attribute of some object of the class array. In the console.log(), console is a class to write some data to the stream, and log() is a method, that prints your data (like 'hello world') to the stream.

Everything was clear?

Section 1. Chapter 3
toggle bottom row

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:

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:

You can store some function arguments.

Task

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.

Task

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.

Actually you already met attributes and methods in other objects. For example, length is a attribute of some object of the class array. In the console.log(), console is a class to write some data to the stream, and log() is a method, that prints your data (like 'hello world') to the stream.

Everything was clear?

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:

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:

You can store some function arguments.

Task

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.

Actually you already met attributes and methods in other objects. For example, length is a attribute of some object of the class array. In the console.log(), console is a class to write some data to the stream, and log() is a method, that prints your data (like 'hello world') to the stream.

Section 1. Chapter 3
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt