The data that we need to receive, process, store, and output to the user is the most decisive element when writing code. Some data are simple, such as those represented by strings (texts from books) or arrays (chat messages), and some are quite complex, such as information about users, which can include dozens or hundreds of properties. Below is an example using a Hexlet student:
// Very simplistic, in reality it's more complicated
const student = {
name: 'Peter',
encryptedPassword: ...,
facebookId: ...,
payments: [/* payment information */],
currentGroup: ...,
finishedCourses: [/* list of courses taken */]
};
One way to handle such data is to build the handler functions, for example, a function for changing the password. This approach is called procedural programming:
// carries out encryption internally and updates the user
// functions mutate user
changePassword(user, 'new secret password');
addFinishedCourse(user, finishedCourse);
Another way is to add methods to objects and use them:
user.changePassword('new secret password');
user.addFinishedCourse(course);
Object-oriented programming refers to the approach in which the code is a collection of objects that interact with one another (OOP). Objects, in this approach, are not simply the data type “object”, they're entities that have behavior, i.e., methods for working with them.
Studying OOP
Object-oriented programming is a vast subject that pervades all aspects of JavaScript development. It is the subject of several courses that cover a wide range of topics, from syntactic constructions to approaches to code organization.
This course introduces basic concepts and syntax and touches a little on the features of JavaScript that make it stand out from other OOP languages. The others are more about the principles of building OOP-style programs and, finally, the insides of JS itself, such as prototypes.
Main topics:
- Objects
- Classes
- Encapsulation
- Data hiding
- Exceptions
These topics are extremely important even for novice JS developers because they'll start dealing with these concepts quite literally from the first day of their new job. On the other hand, it takes a lot of time before you can really make good use of the approaches and techniques you're learning. We will return to the object-oriented programming topic more than once in our courses and deepen not only our understanding of it but also deal with its syntactical matters, such as constructors and prototypes.
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
- Article “How to Learn and Cope with Negative Thoughts“
- Article “Learning Traps“
- Article “Complex and simple programming tasks“
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.