Course Content
Object-Oriented Programming in JavaScript
Object-Oriented Programming in JavaScript
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:
figure1 = new Rectangle(); figure2 = new Rectangle();
There are two different objects, which are instances of Rectangle()
class.
Now we’ll create two students:
student1 = new Student(); student2 = new Student();
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:
student1.name = 'Julia'; student1.age = 18; console.log('Age of the first student is:', student1.age);
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:
Student { // name of the class name: undefined, // attribute and its value university: undefined, age: undefined, gpa: undefined }
There is a similar syntax to call the method:
student2.sleep();
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:
class Student{ name; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ console.log('Today is my birthday!'); } }
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.
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.
Thanks for your feedback!
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:
figure1 = new Rectangle(); figure2 = new Rectangle();
There are two different objects, which are instances of Rectangle()
class.
Now we’ll create two students:
student1 = new Student(); student2 = new Student();
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:
student1.name = 'Julia'; student1.age = 18; console.log('Age of the first student is:', student1.age);
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:
Student { // name of the class name: undefined, // attribute and its value university: undefined, age: undefined, gpa: undefined }
There is a similar syntax to call the method:
student2.sleep();
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:
class Student{ name; university = 'MIT'; age = 18; gpa; sleep(){ console.log('I am sleeping right now!'); } happyBirthday(){ console.log('Today is my birthday!'); } }
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.
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.
Thanks for your feedback!