DEV Community

Srividya Mysore Venkatesh
Srividya Mysore Venkatesh

Posted on

Chapter 6: The Secret Life of Objects

This is the sixth blog out of the twenty-one Blog series. This series is an 'encapsulation'(pun intended) of the concepts learned in the book Eloquent JS.
The sixth chapter is about object-oriented programming concepts.
The objective of objective Oriented Programming is to achieve modularisation.
Encapsulation:
Using modularisation some knowledge about the way a piece of the program works can be kept local to that piece. Someone working on the rest of the program does not have to remember or even be aware of that knowledge. Whenever these local details change, only the code directly around it needs to be updated.

Different pieces of such a program interact with each other through interfaces.

Properties that are part of the interface are called public. The others, which outside code should not be touching, are called private.

Separating interface from implementation is a great idea. It is usually called encapsulation. Basically, wrapping up the code like a nice gift box, not mixing up wrapping tape and gifts....that is encapsulation

Methods:
Methods are nothing more than properties that hold function values.Usually, a method needs to do something with the object it was called on. When a function is called as a method—looked up as property and immediately called, as in object.method()—the binding called this in its body automatically points at the object that it was called on.

This Variable:
You can think of 'this' as an extra parameter that is passed in a different way. If you want to pass it explicitly, you can use a function’s call method, which takes the 'this' value as its first argument and treats further arguments as normal parameters.
Each function has its own 'this' parameter.

Prototypes:
In addition to their set of properties, most objects also have a prototype. A prototype is another object that is used as a fallback source of properties.

When an object gets a request for a property that it does not have, its prototype will be searched for the property, then the prototype’s prototype, and so on.

The prototype relations of JavaScript objects form a tree-shaped structure, and at the root of this structure sits Object.prototype.

Classes:
JavaScript’s prototype system can be interpreted as a somewhat informal take on an object-oriented concept called classes.

A class defines the shape of a type of object—what methods and properties it has. Such an object is called an instance of the class.

Constructors:
A more convenient way to create objects that derive from some shared prototype is called a constructor. In js if we call a function with the 'new' keyword in front of it, it causes it to be treated as a constructor.
It is a convention to have a function capitalized.
Constructors automatically get a property named prototype, which holds a plain empty object. Represented as Object.prototype.

Overriding Derived Properties:
When a property is added to an object, the property is added to the object itself.
if there is a property by the same name and credentials the property will no longer affect the object.

Overriding properties that exist in a prototype is often a useful thing to do. we can override specific objects and let generic objects take a standard value.

Prototype Interference:
A prototype can be used to add new properties and methods to all objects under its influence.

Happy Reading!
SriV

Discussion (0)