Skip to content
    logo
  • About Us
  • Services
    • AI Integration
    • Native Mobile Apps
    • Hybrid App Development
    • Web Development
  • Technologies
    • Mobile
      • Android App Development
      • iOS App Development
      • Flutter
      • React Native
    • Node Js
    • React js
    • WordPress
  • Our Work
  • Blog
  • Career
  • Contact Us
  • zluck instagram zluck instagram zluck linkedin zluck linkedin zluck twitter zluck twitter zluck skype zluck skype zluck github zluck github
zluck instagram zluck instagram zluck linkedin zluck linkedin zluck twitter zluck twitter zluck skype zluck skype zluck github zluck github
Web Development

Javascript OOP concepts

January 25, 2023 — 2 min read

Divyesh kakrecha

Team zluck

Blog detail banner image

In JavaScript, object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. The main concepts of OOP in JavaScript are:

  • Objects: An object is a collection of properties (variables) and methods (functions) that represent a real-world entity or concept. Objects can be created using object literals, constructors, or classes (in ECMAScript 6 and later versions).
     
    var emp = new Object();  
    emp.id = 101;  
    emp.name = "xyz";  
    emp.salary = 50000;  
    document.write(emp.id+" "+emp.name+" "+emp.salary);   
    

  • Properties: Objects have properties, which are variables that store data. Properties can be accessed using the dot notation (object.property) or the square bracket notation (object[“property”]).

    const person = {
      fname:" John",
      lname:" Doe",
      age: 25
    };
    for (let x in person) {
      txt += person[x];
    }
    

  • Methods: Objects have methods, which are functions that perform actions on the object’s properties. Methods can be invoked using the dot notation (object.method()) or the square bracket notation (object”method”).
     
    // creating an object
    let student = { };
    
    // adding a property
    student.name = 'John';
    
    // adding a method
    student.greet = function() {
        console.log('hello');
    }
    
    // accessing a method
    student.greet(); // hello
    

  • Encapsulation: Encapsulation is the practice of keeping the object’s internal state and behavior hidden from the outside world, and exposing only a public interface for interacting with the object. This allows for greater control over the object’s behavior and can help prevent unintended changes to the object’s state.
     
    class Student {  
        constructor() {  
           var name;  
           var marks;  
        }  
        getName() {  
            return this.name;  
        }  
        setName(name) {  
            this.name=name;  
        }   
        getMarks() {  
            return this.marks;  
        }  
        setMarks(marks) {  
            this.marks=marks;  
        }  
    }  
    var stud=new Student();  
    stud.setName("John");  
    stud.setMarks(80);  
    document.writeln(stud.getName()+" "+stud.getMarks()); 
    

  • Inheritance: Inheritance is the mechanism by which one object can inherit the properties and methods of another object. In JavaScript, inheritance can be implemented using the prototype chain.
     
    class Car {
      constructor(brand) {
        this.carname = brand;
      }
      present() {
        return 'I have a ' + this.carname;
      }
    }
    class Model extends Car {
      constructor(brand, mod) {
        super(brand);
        this.model = mod;
      }
      show() {
        return this.present() + ', it is a ' + this.model;
      }
    }
    let myCar = new Model("Ford", "Mustang");
    document.getElementById("demo").innerHTML = myCar.show();
    

  • Polymorphism: Polymorphism is the ability of an object to take multiple forms. In JavaScript, polymorphism can be achieved by using the same method or property name in different objects, and having each one implement the behavior in a unique way.
     
    class firstClass {
        add() {
            console.log("First Method")
        }
    }
    class secondClass extends firstClass {
        add() {
            console.log(30 + 40);
        }
    }
    class thirdClass extends secondClass {
        add() {
            console.log("Last Method")
        }
    }
    var ob = new firstClass();
    var ob2 = new secondClass();
    var ob3 = new thirdClass();
    ob.add();
    ob2.add();
    ob3.add();
    

  • Class: ECMAScript 6 and later versions of javascript introduced classes, this is a blueprint for creating objects (a specific data structure), defining initial values for state (member variables or properties), and executing behavior (member functions or methods).
     
    class Car {
      constructor(name, year) {
        this.name = name;
        this.year = year;
      }
      age() {
        let date = new Date();
        return date.getFullYear() - this.year;
      }
    }
    let myCar = new Car("Ford", 2014);
    document.getElementById("demo").innerHTML =
    "My car is " + myCar.age() + " years old.";
    

Conclusion:

Overall, the concepts of OOP in JavaScript are similar to those in other programming languages and provide a powerful way to organize and structure code, making it more readable, reusable, and maintainable.

You Might Also Like:

blog img
user img
Team zluck

December 18, 2024

Web Development

Why Optimizing Your Website for Phones in 2025 Is Crucial for Business Success

Introduction In 2025, mobile phones are no longer just gadgets—they’re extensions of our daily lives. From shopping and learning to entertainment and communication, mobile devices are the go-to tools for everything. As over 75% of internet users access websites through their phones, businesses must focus on creating seamless mobile experiences. Mobile optimization is more than […]

blog img
user img
Team zluck

December 16, 2024

Web Development

10 Awesome Website Design Ideas You Should Experiment in 2025

Introduction As the digital landscape continues to evolve, website design must adapt to meet the changing expectations of users and take advantage of new technologies. In 2025, it’s not just about creating a visually appealing website, but offering an interactive, personalized, and innovative experience. From augmented reality features to AI-powered personalization, the future of web […]

blog img
user img
Team zluck

November 21, 2024

Web Development

List of Best Practices for a Lead generating Website

In today’s highly competitive digital landscape, a strong online presence is essential for small and medium-sized enterprises (SMEs). Your website should not just serve as a digital business card but act as an active tool to engage visitors, build trust, and convert them into leads. This guide will walk you through the key elements needed […]

View All
zluck

Zluck is an international IT firm that specializes in all types of web, mobile, and digital media solutions.

India

India

+91 97262 50544

zluck instagram zluck instagram zluck linkedin zluck linkedin zluck twitter zluck twitter zluck skype zluck skype zluck mail zluck mail

Services

  • AI Integration
  • Native Mobile Apps
  • Hybrid App Development
  • Web Development

Technologies

  • Android App Development
  • iOS App Development
  • Flutter
  • React Native
  • Node Js
  • React js
  • WordPress

About

  • Zluck Solutions
  • Our Great Works!
  • Our Special Insights
  • Explore Careers

Services

  • AI Integration
  • Native Mobile Apps
  • Hybrid App Development
  • Web Development

Technologies

  • Android App Development
  • iOS App Development
  • Flutter
  • React Native
  • Node Js
  • React js
  • WordPress

About

  • Zluck Solutions
  • Our Great Works!
  • Our Special Insights
  • Explore Careers

Get a Free Quote!

Make your statement on the web!

Get a Free Quote

© Zluck Solutions. All rights reserved

  • Contact Us
  • Privacy Policy
  • Terms of Service