Design Patterns (JS) Flashcards
Pattern to solve the problem of having service layers
module pattern
pattern for overly complicated object interfaces
facade pattern
pattern for better visibility into state changes
observer pattern
types of patterns
creational
structural
behavioural
creational patterns
Constructor (js specific)
Module (js specific)
Factory
Singleton
structural patterns
Decorator
Facade
Flyweight
behavioural patterns
Command
Mediator
Observer
Ways to create an Object
var newObj = {}; var newObj = Object.create(Object.prototype); // will come up around inheritance var newObj = new Object(); // a little out of favor, use class instead
Ways of assigning value to objects
. notation
obj.param = ‘new value’
[] notation
obj[‘param’] = ‘new value’ //one big benefit is being able to use variables inside []
defineProperty on object allows to…
add some additional settings to the objects properties, for example if it can be enumerated, configured or writeable
Object.defineProperty (object, ‘param’,{
value: whatevervalue,
writable: false, //can’t overwrite this later with object.param assignment
enumerable: false //won’t be listed among the params of the object
configurable: false //won’t allow later defineProperty amendments
})
Inherit an object from another object
var obj1 = {whatever};
var obj2 = Object.create(obj1) //copies obj1
Constructor pattern is used to
create new objects with their own object scope
What happens when you put “new” in front of the function
Creates a constructor function: a new object, links to an object prototype, binds “this” to the object scope, returns “this”
Create prototype of the object
ObjectName.prototype.methodName = function…
Why use prototype?
To abstract large functions and avoid repeating them every time we create a new instance of the object. Prototype allows to reference these chunky pieces of code in one place without creating a copy of them with each new instance of the object.
Module pattern in a nutshell
Simple way to encapsulate Methods
Creates a “Toolbox” of functions to use