SOLID / DRY principles

In software development, Object-Oriented Design plays a crucial role when it comes to writing flexible, scalable, maintainable, and reusable code. There are so many benefits of using OOD but every developer should also have the knowledge of the SOLID principle for good object-oriented design in programming. The SOLID principles are designed to help developers design robust, maintainable applications. The five SOLID principles are: Single-responsibility principle - Function and classes should have one job. Open–closed principle - Classes and functions should be open for  extension but not for modification. Liskov substitution principle - Child class can replace parent class Interface segregation principle - This principle is the first principle that applies to Interfaces instead of classes in SOLID and it is similar to the single responsibility principle. It states that “do not force any client to implement an interface which is irrelevant to them“. Here your main goal is ...

Possible ways to create objects in Java Script

There are multiple ways to create objects in JavaScript. Here are some common approaches:

1. Object Literal:
   - The simplest way to create an object is by using the object literal syntax, represented by curly braces `{}`.
   - Example:

  
   const person = {
     name: "John",
     age: 30,
     greet: function() {
       console.log("Hello!");
     },
   };

   console.log(person.name); // Output: John
   person.greet(); // Output: Hello!


   In this example, an object `person` is created using object literal syntax. It has properties `name` and `age`, and a method `greet`.

2. Constructor Functions:
   - Constructor functions are used to create multiple instances of objects with similar properties and behaviors.
   - They are defined using a function that is invoked with the `new` keyword.
   - Example:

   ```javascript
   function Person(name, age) {
     this.name = name;
     this.age = age;
     this.greet = function() {
       console.log("Hello!");
     };
   }

   const person1 = new Person("John", 30);
   console.log(person1.name); // Output: John
   person1.greet(); // Output: Hello!

   const person2 = new Person("Alice", 25);
   console.log(person2.name); // Output: Alice
   person2.greet(); // Output: Hello!
 

   In this example, a constructor function `Person` is defined, which creates instances of `Person` objects with the provided `name` and `age` values. Each instance has its own set of properties and methods.

3. Object.create():
   - The `Object.create()` method allows you to create a new object with a specified prototype object.
   - Example:


   const personPrototype = {
     greet: function() {
       console.log("Hello!");
     },
   };

   const person = Object.create(personPrototype);
   person.name = "John";
   person.age = 30;

   console.log(person.name); // Output: John
   person.greet(); // Output: Hello!


   In this example, an object `personPrototype` is created as a prototype with a `greet` method. The `Object.create()` method is used to create a new object `person` that inherits from `personPrototype`. Additional properties like `name` and `age` can be added to the new object.

4. ES6 Classes:
   - ES6 introduced the `class` syntax, which provides a more structured way to define objects and their behaviors.
   - Example:


   class Person {
     constructor(name, age) {
       this.name = name;
       this.age = age;
     }

     greet() {
       console.log("Hello!");
     }
   }

   const person = new Person("John", 30);
   console.log(person.name); // Output: John
   person.greet(); // Output: Hello!


   In this example, a `Person` class is defined using the `class` keyword. It has a constructor and a `greet` method. Instances of the class can be created using the `new` keyword.

These are some of the common ways to create objects in JavaScript. Each approach has its advantages and use cases, depending on the specific requirements of your code.

Comments

Popular posts from this blog

Difference between localStorage and sessionStorage

Scope in javascript

What is memoization