Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Objects' Creating | 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

book
Objects' Creating

The object is an instance of some class. For example, for our class Rectangle, which describes some figures, we can create two figures:

12
figure1 = new Rectangle(); figure2 = new Rectangle();
copy

There are two different objects, which are instances of Rectangle() class.

Now we’ll create two students:

12
student1 = new Student(); student2 = new Student();
copy

These two objects have predefined methods and attributes. name, university, gpa, and age have undefined values.

To init or change the attribute’s value for an object, you can simply refer to it. Same you can do if you want to get the value of this attribute:

123
student1.name = 'Julia'; student1.age = 18; console.log('Age of the first student is:', student1.age);
copy

There is a simple way to detect which values object's attributes have: call console.log(object_name) and you'll see the JSON string that looks like:

123456
Student { // name of the class name: undefined, // attribute and its value university: undefined, age: undefined, gpa: undefined }
copy

There is a similar syntax to call the method:

1
student2.sleep();
copy

Note that you can't create an instance of the class before the defintion of this class.

You can set some default name for attribute inside a class:

123456789101112
class Student{ name; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ console.log('Today is my birthday!'); } }
copy

Now all objects will have undefined value of gpa and name, but predefined values of age and university.

You refer to methods and some single object attributes, so you’ll work with data in it.

Each object has a unique value of attributes, i. e. if you change the name for student1, the name of student2 doesn’t change.

Class notation is a formal and common representation of the behavior of different objects of this class.

Task
test

Swipe to show code editor

Set some default values for all Student class attributes. Then create object of class Student and output values of all the attributes for this object. In the end, call the sleep() method.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
toggle bottom row

book
Objects' Creating

The object is an instance of some class. For example, for our class Rectangle, which describes some figures, we can create two figures:

12
figure1 = new Rectangle(); figure2 = new Rectangle();
copy

There are two different objects, which are instances of Rectangle() class.

Now we’ll create two students:

12
student1 = new Student(); student2 = new Student();
copy

These two objects have predefined methods and attributes. name, university, gpa, and age have undefined values.

To init or change the attribute’s value for an object, you can simply refer to it. Same you can do if you want to get the value of this attribute:

123
student1.name = 'Julia'; student1.age = 18; console.log('Age of the first student is:', student1.age);
copy

There is a simple way to detect which values object's attributes have: call console.log(object_name) and you'll see the JSON string that looks like:

123456
Student { // name of the class name: undefined, // attribute and its value university: undefined, age: undefined, gpa: undefined }
copy

There is a similar syntax to call the method:

1
student2.sleep();
copy

Note that you can't create an instance of the class before the defintion of this class.

You can set some default name for attribute inside a class:

123456789101112
class Student{ name; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ console.log('Today is my birthday!'); } }
copy

Now all objects will have undefined value of gpa and name, but predefined values of age and university.

You refer to methods and some single object attributes, so you’ll work with data in it.

Each object has a unique value of attributes, i. e. if you change the name for student1, the name of student2 doesn’t change.

Class notation is a formal and common representation of the behavior of different objects of this class.

Task
test

Swipe to show code editor

Set some default values for all Student class attributes. Then create object of class Student and output values of all the attributes for this object. In the end, call the sleep() method.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
Switch to desktopSwitch 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