Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Objects' Creating | Classes & Objects
Object-Oriented Programming in JavaScript

Stryg for at vise menuen

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.

Opgave

Swipe to start coding

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4
Vi beklager, at noget gik galt. Hvad skete der?

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

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.

Opgave

Swipe to start coding

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 4
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt