Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Javascript Design Patterns You Have to Know

The Javascript Design Patterns You Have to Know

My talk at: JOSA TechTalks - JS Patterns & Frameworks

http://www.meetup.com/JOSA-TechTalks/events/219374144/

Source code:
https://github.com/rashad612/josaTT-JSPatterns

Rashad Majali

January 13, 2015
Tweet

Other Decks in Technology

Transcript

  1. Prototype-based JS •JavaScript is OO. •JavaScript is an object-based language

    based on prototypes not a class-based. •There are no classes, but constructor functions and template objects to create new objects. •Everything is an object.
  2. Constructor Design Pattern •You can create a new empty object

    using the following methods: •Calling a constructor function by “new” keyword. •Preferably to be used with “prototypes”. •Inside the constructor “this” keyword will reference the newly created object.
  3. Prototype Design Pattern •Template objects (prototype) for every new objects

    created by constructors. •Prototypes is an easy way for inheritance between objects. •It comes with high performance benefits. •Prototypal Inheritance in ES5 can be done using
  4. Module Design Pattern •Modules are set of components in an

    application architecture, integrating, communicating with each other keeping units of code clean and separated. •Modules in JS implemented by: The Module pattern Object literal notation AMD modules CommonJS modules ECMAScript Harmony modules
  5. Module Design Pattern •In JS we use Module pattern, to

    have a public/private methods and attributes encapsulated in a single object. •Encapsulation is made using closures. •Closures only returns public API to the global scope. •Export an object.
  6. Module Design Pattern •Examples: ◦ Module Literal: src/moduleLiteral.js ◦ Simple

    Module: src/moduleSimple.js ◦ Module with imports/exports: src/moduleIO.js
  7. Module Design Pattern •Advantages: ◦ Cleaner than constructor pattern for

    encapsulation. ◦ The ability to have private methods/attributes. •Disadvantages: ◦ Changing visibility can be expensive. ◦ After closure, if we added a new public method it won’t see private members.
  8. Module Design Pattern •The Revealing Module Pattern: ◦ Christian Heilmann.

    ◦ All functions and variables in the private scope of the closure, with naming convention. ◦ Only specific can be exported publicly using anonymous object. ◦ Example: src/moduleReveal.js
  9. Module Design Patterns •The Revealing Module Pattern: ◦ Advantages: Consistent

    way of defining the module and visibility. ◦ Disadvantages: Modules are not stable, and might not be tested easily.
  10. References: •Details of the object model | MDN •Introduction to

    Object-Oriented JavaScript | MDN •Learning JavaScript Design Patterns, Addy Osmani, Volume 1.6.0 •JavaScript Design Patterns, Carl Danley •JavaScript Module Pattern: In-Depth