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

Scope in javascript

In JavaScript, scope refers to the visibility and accessibility of variables, functions, and objects in a particular part of your code. It determines where and how variables can be accessed and modified. There are two main types of scope in JavaScript: global scope and local scope.


1. Global Scope:

   - Variables declared outside of any function have global scope.

   - Global variables can be accessed from anywhere in your code, including inside functions.

   - Example:



   var globalVariable = "I'm a global variable";


   function printGlobal() {

     console.log(globalVariable);

   }


   printGlobal(); // Output: I'm a global variable



2. Local Scope:

   - Variables declared within a function have local scope, meaning they are only accessible within that function (including any nested functions).

   - Local variables take precedence over global variables with the same name.

   - Example:


  

   function printLocal() {

     var localVariable = "I'm a local variable";

     console.log(localVariable);

   }


   printLocal(); // Output: I'm a local variable

   console.log(localVariable); // ReferenceError: localVariable is not defined

 

   In the above example, `localVariable` is accessible within the `printLocal` function, but outside of it, an attempt to access it results in a `ReferenceError` because the variable is not defined in the outer scope.


 

   function outerFunction() {

     var outerVariable = "I'm an outer variable";


     function innerFunction() {

       var innerVariable = "I'm an inner variable";

       console.log(outerVariable); // Accessible from the inner function

     }


     innerFunction();

     console.log(innerVariable); // ReferenceError: innerVariable is not defined

   }


   outerFunction(); // Output: I'm an outer variable

 

   In this example, `outerVariable` is accessible within both the outer and inner functions, while `innerVariable` is only accessible within the inner function.


It's important to note that each function has its own scope, and variables declared within a function are not accessible from outside that function. Additionally, modern JavaScript introduced the `let` and `const` keywords, which provide block scope, meaning variables declared with them are limited to the block in which they are defined (such as inside `if` statements or loops).


Understanding scope is essential for writing maintainable and bug-free JavaScript code, as it helps you manage variable accessibility and prevent naming conflicts between different parts of your program.

Comments

Popular posts from this blog

Difference between localStorage and sessionStorage

What is memoization