In this blog, we take an in-depth look at JavaScript, a prototype-based procedural language that incorporates functional and object-oriented programming paradigms. We also explore how JavaScript uses the concepts of classes, objects, encapsulation, and inheritance, and learn about ES6 classes, function scope, closures, and prototypal inheritance.
JavaScript is a Prototype-based programming paradigm. It is not an OOP language. But it has support for it. It has the class keyword, which is just a syntactic sugar over prototypes. We actually create prototypes in javascript, and not classes. The class keyword is there to make javascript look like an OOP language.
Javascript uses objects and follows the principles of OOP, but it does this using pure functions, which is a property of the functional programming paradigm (FP). JavaScript follows both OOP and FP and is actually a procedural language.
Note: JavaScript is not an object-oriented language. Neither is it completely a functional language. JavaScript is a prototype-based procedural language. It supports both functional and object-oriented patterns of programming.
There are four rules or main pillars of Object-oriented programming language. This defines how the data and actions associated with the data; are organized using code.
JavaScript is a prototype-based language, and it doesn't have classes in it. We define the templates for objects using constructor functions or prototypes.
But JavaScript does have support for the class keyword.
Let's look at the ES6 way of defining classes, using the class keyword and then we'll see how classes were defined traditionally.
This way, we create a class using the class keyword. Define constructor function and all the other data functions inside the class declaration function. Then we create instances for this class using the new keyword.
This is how classes were defined before JavaScript started supporting the class keyword. That is before ES6 came up.
An object is a data structure containing properties and methods. Consider a student. A student will have characteristics like name, roll number, and class, and he will perform an action, let's say, giving an exam. In object-oriented programming, these characteristics are called data variables. These actions are called data methods.
We create a class called Student and then create instances of this class. These instances are called Objects. Hence, an object is a real instance of a class. Class is nothing but a prototype/blueprint.
Object can be created in two ways in JavaScript:
Encapsulation puts the data variables and the data functions together inside a box. Encapsulation ensures that data can only be accessed using the data functions defined inside the class, and abstraction ensures that no one outside this encapsulated box can access it.
But, we already saw that there is no actual concept of classes in JavaScript. So, JavaScript implements encapsulation using two ways: Function Scope and Closures.
Let's discuss both of them with examples.
When we define a variable or a function inside the function, we can only access it from inside and not from outside the function. This can be one way of implementing abstraction. Look at the example below.
Output:
The output for this code snippet says the right message when accessed from inside but throws an error when accessed from outside. Hence, the message is encapsulated.
We can create a function inside a function, and the inner function will be able to access the local variable defined inside the outer function. This is called closure.
Note: A closure is created every time a function is declared.
Output:
Again, the output says that creating a closure helps encapsulate and restrict the data access only to inside the function.
JavaScript lets objects inherit properties from parent objects or any other objects. It uses the concept of prototypal inheritance.
JavaScript is a prototype-based language. The objects created here can inherit the properties and methods from other objects or their parent ones.
Let's create an object and console it.
We only defined the property name, age & mobile to the user Object, but we can see a lot of other methods. We didn't define them! These methods are inherited from the parent object, i.e., the prototype for the object data structure. When we create the object user Object, we basically inherit all the properties object data structure.
In our next blog, we compare and contrast asynchronous programming in Dart and JavaScript, discussing their similarities and differences.