Skip to content

Instantly share code, notes, and snippets.

@jaimelr
Created April 12, 2019 21:37
Show Gist options
  • Save jaimelr/dddf4a79b009b058399bafe5c6d3f10c to your computer and use it in GitHub Desktop.
Save jaimelr/dddf4a79b009b058399bafe5c6d3f10c to your computer and use it in GitHub Desktop.
JavaScript Design Patterns

Common Object Patterns

Function Arguments

Arguments aren't required like .NET languages, like all variables in JavaScript arguments are untyped.

Argument Object

Available in all functions, like an array, but not really and array (cannot sort or filter)

argument[0]

Channing

new Calc(0)
  .add(1)
  .add(2)
  .multiply(3)

Observable properties

Javascript properties are really just public fields

Use methods-as-properties

Using ES5 Properties

Object.defineProperty(this, 'name', {
  get: function() {
  
  }
})

Timer Patterns

Executes the function once

setTimeout(function () { ... }, 0)

Executes continuously with the specified delay between each execution.

setInterval(function () { ... }, 0)

Delays of <4ms will be bumped to 4 ms

Timers wont start until the outer most function is finished.

Asynchronous Execution Pattern

Split long-running javascript blocks using SetTimeout

Recursive setTimeout Pattern

Executes code of continuos period of time

Most commonly when querying a data source

Asynchronous Module Definitions

Javascript lacks any way to have external references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment