Trending September 2023 # Object Oriented Programming In Javascript # Suggested October 2023 # Top 10 Popular | Speedmintonvn.com

Trending September 2023 # Object Oriented Programming In Javascript # Suggested October 2023 # Top 10 Popular

You are reading the article Object Oriented Programming In Javascript updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Object Oriented Programming In Javascript

Introduction to Object Oriented Programming in JavaScript

JavaScript, also abbreviated as JS, is a high-level, interpreted programming language. JavaScript is one of the three main technologies of the World Wide Web. JavaScript enables to build interactive web pages other than static web pages. Most websites use JS for development, and major web browsers have a JavaScript engine to execute it. JavaScript is a lightweight, weakly typed, prototype-based interpreted programming language with object-oriented capabilities. JavaScript is used for client-side development of web applications; it is included in or referenced by an HTML file so that the code is rendered in the browser.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Concepts of Object Oriented Programming in JavaScript

Whether JavaScript is a pure Object-Oriented language or not is always debatable. Some say it is not Object-Oriented because it has no classes and cannot implement OOPs concepts like inheritance or encapsulation. Some say that it is Object-Oriented language. Before discussing the topic, one should understand what does make a programming language an Object-Oriented programming language. There are a few important characteristics of Object-Oriented Languages:

1) Polymorphism: 

The ability of the object to take many forms. For example, the function can be overloaded with the same name but different parameters.

2) Encapsulation: 

Binding all the data and the operations together and keeping it in a class.

3) Inheritance:

Child Class can be derived from parent class with all the features from the parent class and some properties of its own.

These three principles form the basis of an Object-Oriented language. So, JavaScript might not follow the exact paradigm of Object-Oriented Principles, but JavaScript has different ways of implementing the OOP.

Examples of Object Oriented Programming in JavaScript

We can demonstrate how JavaScript follows Object-Oriented Principles by taking some examples:

Encapsulation

The idea of encapsulation is that an object’s data should not be accessed directly; instead, it should be accessed via some methods. But in JavaScript, there is no concept of class and objects it implements encapsulation in two ways that is Function Scope and Closure.

1. Function Scope

This is nothing but declaring the variables inside the functions. So, the scope of the variables will be limited only to functions, and other objects cannot access the variables. Let’s take an example to demonstrate the function scope.

function test() { var value = "Hello!"; alert( value) } alert( value) 2. Closures var person = { var name = "JavaScript"; return  { setName : function(value){ name = value; }, getName : function(){ return name; } }; }; alert(person.name) person.setName("Java") alert(person.name)

So, in this way, we can declare inner objects or methods to hide the data and those can be accessed using parent objects.

Inheritance

JavaScript supports inheritance through the concept called Prototypal inheritance allows JavaScript objects to acquire all or some of the features from other objects or parent objects.

Prototype

JavaScript is described as a Prototype-based language, to provide an inheritance object that can have a prototype object that inherits methods and properties from other objects. An object’s prototype object may also have a prototype object which inherits methods and properties from its parent object and so on, this is called a prototype chain.

The image above shows all the properties of the object “animal” that is being created. So, the “name” and “type” are the properties of an object “animal” and the properties like value Of, to String, toLocaleString, etc are the properties of a parent object that are inherited by the animal object. Refer to the below image that shows the properties of the parent object using the property proto.

Polymorphism

Poly means “many”, morphism means “forms”, and polymorphism is nothing but having different forms. In JavaScript polymorphism is achieved by generic, overloading, and structural subtyping.

1. Generics (Parametric Polymorphism)

data(3)([1,2]);   data(“c”)([“a”, “b”]);   2. Overloading (ad-hoc polymorphism)

Overloading in JavaScript is achieved using as-hoc polymorphism. Ad-hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of several types because polymorphic functions can denote the number of distinct heterogeneous implementations depending on the types of the arguments. Refer to the below-mentioned example

The “+” operator is used in several ways like adding the numbers, concatenating the strings, etc.

1 + 2 = 3

1.2+2.3 = 3.5

“Java”+”Script” = “JavaScript”

3. Structural subtyping (Structural Polymorphism)

Structural polymorphism says that different types are equivalent, for instance if one type has all the properties of the other type along with some additional properties (following the same structure)

Const weight  = {value:100, data:true}

Const speed = {value:200, data:false, foo:[1,2,3]}

Speed is considered to be the subtype of weight.

Conclusion

So, now we can conclude that JavaScript is an Object-Oriented Language. Though it doesn’t have any real classes it is still an Object-Oriented Language because it follows the core concepts of Object-Oriented principles. So, a language can be Object Oriented if it supports objects even without classes.

Recommended Articles

This is a guide to Object Oriented Programming in JavaScript. Here we discuss the different concepts and the applications of Object Oriented Programming in JavaScript. You may also look at the following article to learn more –

You're reading Object Oriented Programming In Javascript

Update the detailed information about Object Oriented Programming In Javascript on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!