Skip to content

Instantly share code, notes, and snippets.

@shivam1283
Last active May 7, 2022 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivam1283/4239931f8d382a38894acea9a4f02288 to your computer and use it in GitHub Desktop.
Save shivam1283/4239931f8d382a38894acea9a4f02288 to your computer and use it in GitHub Desktop.
[Javascript design patterns] #js #development

Pattern is a reusable solution to a commonly occuring problem. A pattern is a reusable solution that can be applied to commonly occurring problems in software design. templates for how we solve problems

Types of design pattern

  1. Creational pattern
  2. Structural pattern
  3. Behavioural pattern

Benefits

  • Patterns are proven solutions.
  • Patterns can be easily reused.
  • Patterns can be expressive - a set structure and vocabulary to the solution presented that can help express rather large solutions quite elegantly.

Other benefits

  • Reusing patterns assists in preventing minor issues that can cause major problems in the application development process.
  • Patterns can provide generalized solutions which are documented in a fashion that doesn't require them to be tied to a specific problem.
  • Certain patterns can actually decrease the overall file-size footprint of our code by avoiding repetition.

A constructor is a special method to initialize the object with values and methods once the object is allocated a memory. JS uses Object as constructor.

Object constructors are used to create specific types of objects - both preparing the object for use and accepting arguments which a constructor can use to set the values of member properties and methods when the object is first created.

  • Object creation mechanisms and defining object properties

Ways to create an object in JS;

  1. Curly brackets const obj = {}

  2. 'new' Notation const obj = new Object()

  3. 'Object.create' const obj = Object.create(Object.prototype)

The Module pattern was originally defined as a way to provide both private and public encapsulation for classes in conventional software engineering.

Object literals{} as modules

 
  myProperty: "someValue",
 
  // object literals can contain properties and methods.
  // e.g we can define a further object for module configuration:
  myConfig: {
    useCaching: true,
    language: "en"
  },
 
  // a very basic method
  saySomething: function () {
    console.log( "Where in the world is Paul Irish today?" );
  },
 
  // output a value based on the current configuration
  reportMyConfig: function () {
    console.log( "Caching is: " + ( this.myConfig.useCaching ? "enabled" : "disabled") );
  },
 
  // override the current configuration
  updateMyConfig: function( newConfig ) {
 
    if ( typeof newConfig === "object" ) {
      this.myConfig = newConfig;
      console.log( this.myConfig.language );
    }
  }
};
  • Privacy

    • This module encapsulates privacy, states and organisation using closures.
    • Prevents leaking variables and methods to leak into global scope and colliding with other developer's interface.
    • This patterns returns a public API and everything else is private. This gives a clean solution to the problem while hidding the heavy lifting logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment