Object Oriented Programming in JavaScript

SeanUdayantha
5 min readFeb 26, 2022

--

What is object-oriented programming?

Object-oriented programming (OOP) is a programming model that organizes software design around data, rather than functions and logic. An object is a data field with its own set of properties and behavior.

The most popular model of OOP is class-based.

But as I mentioned, JavaScript isn’t a classed-based language — it’s is a prototype-based language.

what is the prototype-based language?

A prototypical object is an object that is used as a template to acquire the beginning properties for a new object in a prototype-based language.

Object-oriented programming concepts and fundamentals

Classes

Classes are essentially user defined data types. Basically a class is a blueprint or template from which an object is instantiated or created.

Objects

Objects are instance of classes. These are entities that has specific state and behavior. It can be a variable, a data structure, a function or a method.

Attributes

Attributes are the information that is stored. Attributes are defined in the class template. When objects are instantiated individual objects contain data stored in the Attributes field.

Methods

Methods represent behaviors. They perform some actions. Methods might return information about an object, or update an object’s data. The method’s code is defined in the class definition.

Principles of OOP

Object-Oriented Programming is built on four pillars:

· Inheritance — It is the mechanism by which one class is allowed to inherit the features of another class.

· Encapsulation — It is defined as wrapping up of data in a single unit. It is a mechanism that binds together code and data it manipulates.

· Abstraction — It is a process of hiding the implementation details from the user.

· Polymorphism — It refers to the ability of an object to take more than one forms.

The First 5 Principles of Object Oriented Design.

S.O.L.I.D

Single responsibility

A class should have only one cause to change, which means it should only have one task.

Open-close

Objects or entities should be able to be extended but not modified.

Liskov substitution

Let q(x) be a property that can be proved for objects of type T of x. Then, for objects y of type S, where S is a subtype of T, q(y) should be provable.

Interface segregation

Clients should never be compelled to implement an interface they don’t use, nor should they be forced to rely on methods they don’t utilize.

Dependency inversion

Abstractions, not concretions, must be relied upon by entities. It specifies that the high-level module should rely on abstractions rather than the low-level module.

JavaScript

JavaScript, abbreviated JS, is a computer language that, together with HTML and CSS, is one of the essential technologies of the World Wide Web. On the client side, over 97 percent of websites employ JavaScript for web page behavior, typically in conjunction with third-party libraries. All major web browsers have a JavaScript engine that executes code on users’ devices.

Classes and objects

Classes are “special functions,” thus the class syntax, like function expressions and function declarations, includes two components: class expressions and class declarations.

Objects in JavaScript, like items in many other programming languages, may be compared to real-world objects. The idea of objects in JavaScript may be grasped by comparing it to real-world, physical items.

prototypes

JavaScript objects inherit features from one another through the use of prototypes. In this post, we will define a prototype, explain how prototype chains function, and show how to create a prototype for an object.

this key word

This keyword in JavaScript allows us to: Reuse functions in different execution contexts. This implies that once declared, a function may be executed for different objects by utilizing the this keyword. When we execute a method, we must identify the object in the current execution context.

Strict notation

Strict Mode is a new feature in ECMAScript 5 that allows you to run a program or function in a “strict” operating context. This stringent context forbids some actions and creates more exceptions. The sentence “use strict” informs the browser to use JavaScript’s Strict mode, which is a smaller and safer feature set.

Strict mode for functions

Function closure

A closure is a function that has been wrapped (contained) with references to its surrounding state (the lexical environment). In other words, a closure allows you to access the scope of an outside function from an inside function. Closures are formed in JavaScript every time a function is created, during function creation time.

Callbacks and promises

Callbacks: A callback function is one that is embedded within a function that merely receives another function as an argument. Callback functions are a basic functional programming idea present in practically all JavaScript code, whether in simple methods like ’setInterval’ event listening, or performing API calls.

promises: A promise is used to handle the asynchronous result of an action. JavaScript is intended to run other synchronous pieces of code without waiting for an asynchronous block of code to complete. When we make API queries to servers, for example, we have no notion if the servers are offline or up, or how long it takes for the server request to be executed.
Promises may be used to delay the execution of a code block until an async request is fulfilled. As a consequence, other operations can continue undisturbed.

--

--

No responses yet