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 ...

Callback function

A callback function is a function that is passed as an argument to another function and is executed by that function at a specific time or in response to a certain event. The callback function is typically used to customize or define additional behavior for the calling function.


In JavaScript, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This flexibility allows callback functions to be used extensively in JavaScript for handling asynchronous operations, event handling, and functional programming.


Here are the key characteristics of a callback function:


1. Passed as an Argument: The callback function is passed as an argument to another function. It can be defined inline or as a separate named function.


2. Execution by the Calling Function: The calling function is responsible for executing the callback function at an appropriate time. This could be after completing an asynchronous operation, when an event occurs, or as part of the normal control flow.


3. Custom Behavior: The callback function allows you to define custom behavior that gets executed when it is called. It can access variables and parameters from the calling function's scope, providing a way to modify or extend the behavior of the calling function.


4. Invocation by the Calling Function: When the calling function reaches the point where the callback function is supposed to be executed, it invokes the callback function, passing any required arguments.


Callback functions are widely used in JavaScript to handle asynchronous operations, such as making API calls or performing time-consuming tasks. They also play a significant role in event-driven programming, where code execution is determined by user actions or system events.


The use of callback functions enables code modularity, extensibility, and flexibility by allowing different functions to be customized dynamically based on specific needs.

function callbackFunction(name) {
  console.log('Hello ' + name);
}

function outerFunction(callback) {
  let name = prompt('Please enter your name.');
  callback(name);
}

outerFunction(callbackFunction);

Comments

Popular posts from this blog

Difference between localStorage and sessionStorage

Scope in javascript

What is memoization