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

Зміст курсу

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

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

bookConstructor

Constructor is used for object creation and specifying some attributes. Look at this statement:

1
student = new Student();
copy

Student() is a constructor of class Student, so the student variable is now a Student object.

This simple constructor is using by default and creates an object only. But you can define a constructor that will initialize some object attributes.

1
student = new Student('Robert', 22);
copy

student object now has name Robert and age 22.

The syntax of the constructor inside the class is following:

12345678910111213141516171819202122
class Student{ name; university; age; gpa; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

By object creating, you can pass some arguments and use them inside constructor. But note that skipped parameters will have undefined value. Look at the example and run it to see what happens.

123456789101112131415
class Student{ name; age; gpa; constructor(name, age, gpa){ // constructor with 3 parameters this.name = name; this.age = age; this.gpa = gpa; console.log('Student created'); } } student = new Student('Robert', 22); // pass only 2 values console.log(JSON.stringify(student)); // object has only 2 defined properties name and age
copy

Only name and age properties are defined in student object because of 2 arguments while student initialization.

Pass the arguments in order they are defined in constructor.

Завдання
test

Swipe to show code editor

Extend existing constructor with arguments for university and gpa attributes. After that, change the line of object initialization by adding 2 more arguements inside Student() statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 6
toggle bottom row

bookConstructor

Constructor is used for object creation and specifying some attributes. Look at this statement:

1
student = new Student();
copy

Student() is a constructor of class Student, so the student variable is now a Student object.

This simple constructor is using by default and creates an object only. But you can define a constructor that will initialize some object attributes.

1
student = new Student('Robert', 22);
copy

student object now has name Robert and age 22.

The syntax of the constructor inside the class is following:

12345678910111213141516171819202122
class Student{ name; university; age; gpa; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

By object creating, you can pass some arguments and use them inside constructor. But note that skipped parameters will have undefined value. Look at the example and run it to see what happens.

123456789101112131415
class Student{ name; age; gpa; constructor(name, age, gpa){ // constructor with 3 parameters this.name = name; this.age = age; this.gpa = gpa; console.log('Student created'); } } student = new Student('Robert', 22); // pass only 2 values console.log(JSON.stringify(student)); // object has only 2 defined properties name and age
copy

Only name and age properties are defined in student object because of 2 arguments while student initialization.

Pass the arguments in order they are defined in constructor.

Завдання
test

Swipe to show code editor

Extend existing constructor with arguments for university and gpa attributes. After that, change the line of object initialization by adding 2 more arguements inside Student() statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 6
toggle bottom row

bookConstructor

Constructor is used for object creation and specifying some attributes. Look at this statement:

1
student = new Student();
copy

Student() is a constructor of class Student, so the student variable is now a Student object.

This simple constructor is using by default and creates an object only. But you can define a constructor that will initialize some object attributes.

1
student = new Student('Robert', 22);
copy

student object now has name Robert and age 22.

The syntax of the constructor inside the class is following:

12345678910111213141516171819202122
class Student{ name; university; age; gpa; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

By object creating, you can pass some arguments and use them inside constructor. But note that skipped parameters will have undefined value. Look at the example and run it to see what happens.

123456789101112131415
class Student{ name; age; gpa; constructor(name, age, gpa){ // constructor with 3 parameters this.name = name; this.age = age; this.gpa = gpa; console.log('Student created'); } } student = new Student('Robert', 22); // pass only 2 values console.log(JSON.stringify(student)); // object has only 2 defined properties name and age
copy

Only name and age properties are defined in student object because of 2 arguments while student initialization.

Pass the arguments in order they are defined in constructor.

Завдання
test

Swipe to show code editor

Extend existing constructor with arguments for university and gpa attributes. After that, change the line of object initialization by adding 2 more arguements inside Student() statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Constructor is used for object creation and specifying some attributes. Look at this statement:

1
student = new Student();
copy

Student() is a constructor of class Student, so the student variable is now a Student object.

This simple constructor is using by default and creates an object only. But you can define a constructor that will initialize some object attributes.

1
student = new Student('Robert', 22);
copy

student object now has name Robert and age 22.

The syntax of the constructor inside the class is following:

12345678910111213141516171819202122
class Student{ name; university; age; gpa; constructor(name, age){ this.name = name; this.age = age; console.log('Student created'); } sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ this.age += 1; console.log('Today is my birthday'); } } student = new Student('Robert', 22);
copy

By object creating, you can pass some arguments and use them inside constructor. But note that skipped parameters will have undefined value. Look at the example and run it to see what happens.

123456789101112131415
class Student{ name; age; gpa; constructor(name, age, gpa){ // constructor with 3 parameters this.name = name; this.age = age; this.gpa = gpa; console.log('Student created'); } } student = new Student('Robert', 22); // pass only 2 values console.log(JSON.stringify(student)); // object has only 2 defined properties name and age
copy

Only name and age properties are defined in student object because of 2 arguments while student initialization.

Pass the arguments in order they are defined in constructor.

Завдання
test

Swipe to show code editor

Extend existing constructor with arguments for university and gpa attributes. After that, change the line of object initialization by adding 2 more arguements inside Student() statement.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 1. Розділ 6
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt