Difference ways to create an Object following OOP:

Difference ways to create an Object following OOP:

In JavaScript, object creation in Object-Oriented Programming (OOP) can be in several ways. Here are some common methods:

1. Object Literals

- The simplest way to create an object is by using an object literal. This method is quick and straightforward but not as flexible for creating multiple similar objects.

   const car = {

       make: 'Toyota',

       model: 'Corolla',

       year: 2020,

       start: function() {

           console.log('Car started');

       }

   };        

2. Factory Functions

- A factory function returns a new object each time it is called. This is a more scalable approach compared to object literals.

   function createCar(make, model, year) {

       return {

           make: make,

           model: model,

           year: year,

           start: function() {

               console.log(`${this.make} ${this.model} started`);

           }

       };

   }

   const myCar = createCar('Honda', 'Civic', 2022);        


3. Constructor Functions

- Constructor functions allow for creating multiple instances of objects with shared properties and methods. They are conventionally named with a capital letter.

   function Car(make, model, year) {

       this.make = make;

       this.model = model;

       this.year = year;

       this.start = function() {

           console.log(`${this.make} ${this.model} started`);

       };

   }

   const myCar = new Car('Ford', 'Mustang', 2021);        


4. ES6 Classes

- Classes provide a more formal and clean syntax for creating objects, introduced in ECMAScript 2015 (ES6). Under the hood, they work similarly to constructor functions but offer additional features like inheritance and better readability.

   class Car {

       constructor(make, model, year) {

           this.make = make;

           this.model = model;

           this.year = year;

       }

       start() {

           console.log(`${this.make} ${this.model} started`);

       }

   }

   const myCar = new Car('Tesla', 'Model 3', 2023);        


5. Object.create()

- Object.create() creates a new object using an existing object as the prototype. This method allows for prototype-based inheritance.

   const carPrototype = {

       start: function() {

           console.log(`${this.make} ${this.model} started`);

       }

   };

   const myCar = Object.create(carPrototype);

   myCar.make = 'BMW';

   myCar.model = 'X5';

   myCar.year = 2022;        

6. Singleton Pattern

- The Singleton pattern ensures that a class has only one instance and provides a global point of access to it.

   const singletonCar = (function() {

       let instance;

       function createInstance() {

           return {

               make: 'Audi',

               model: 'Q7',

               year: 2022

           };

       }

       return {

           getInstance: function() {

               if (!instance) {

                   instance = createInstance();

               }

               return instance;

           }

       };

   })();

   const carInstance1 = singletonCar.getInstance();

   const carInstance2 = singletonCar.getInstance();

   console.log(carInstance1 === carInstance2); // true        


7. Function Constructors with Prototypes

- In this method, methods are attached to the prototype of the constructor function, so they are shared among all instances.


   function Car(make, model, year) {

       this.make = make;

       this.model = model;

       this.year = year;

   }

   Car.prototype.start = function() {

       console.log(`${this.make} ${this.model} started`);

   };

   const myCar = new Car('Chevrolet', 'Camaro', 2021);        

Each method has its own use cases depending on the requirements of the application, such as whether you need to create multiple objects, share methods across instances, or implement inheritance.

要查看或添加评论,请登录

Towhidul Islam的更多文章

  • The Future of AI Powered Web Solutions: What Businesses Need to Know

    The Future of AI Powered Web Solutions: What Businesses Need to Know

    Artificial Intelligence is transforming the digital landscape, redefining how businesses interact with customers…

    5 条评论
  • Types of HTTP Status code.

    Types of HTTP Status code.

    In server responses, HTTP status codes are used to indicate the outcome of a client's request to the server. These…

  • Setting Up Nginx for Your Domain on Ubuntu

    Setting Up Nginx for Your Domain on Ubuntu

    Setting Up Nginx for Your Domain on Ubuntu This guide will walk you through the process of installing Nginx on an…

  • JavaScript Closures: A Detailed Explanation

    JavaScript Closures: A Detailed Explanation

    What is a Closure? A closure is a feature in JavaScript where an inner function has access to its own scope, the outer…

  • Recursion

    Recursion

    Recursion Recursion is the technique of making a function call itself. This technique provides a way to break…

  • Virtualization.

    Virtualization.

    Virtualization is a technology that enables the creation of virtual instances of physical hardware, operating systems…

  • Time complexity.

    Time complexity.

    Imagine a classroom of 100 students in which you gave your pen to one person. You have to find that pen without knowing…

  • ls -l

    ls -l

    towhidul@towhidul-Aspire-V5-471G:~$ ls -l total 36 drwxr-xr-x 2 towhidul towhidul 4096 Feb 25 16:59 Desktop drwxr-xr-x…

  • Linux Command Line Basics

    Linux Command Line Basics

    Lab Environment Setup One can use an online bash interpreter to run all the commands that are provided as examples in…

  • React Component

    React Component

    React components are an integral part of building user interfaces with React, and they indeed make developers' lives…

社区洞察

其他会员也浏览了