Common Object Patterns
Function argument patterns – begins with magic arguments; Aaron explains the arguments object and how arguments work differently to languages like Java and C# which have method overloading.
The arguments object doesn’t have sorting or filtering capabilities like a genuine array object has. There is an example for iterating through every element within the arguments object. This is a technique used in libraries such as jQuery.
Amongst strongly typed languages, this is a technique that was popularised by LINQ in .NET and later incorporated into the Java Language.
Aaron explains that in .NET we have an INotifyPropertyChanged interface to make observing changes easy.
Creating Observable Properties
Observable Property Futures
Aaron says that as of ECMAScript 5, properties can have method bodies, using the Object.defineproperty method.
He warns that this is only supported in IE9+.
In 2016, it is now a lot more reasonable to expect users to use a browser to supporting ES5, meaning adding method bodies to properties makes more sense today than when the course was recorded in 2011.
The introductory clip introduces two timers: setTimeout and setInterval
Unlike setTimeout, the function setinterval is executed multiple times, once for each Interval as specified.
Timers won’t start until the outer most function has executed
In Timer overview, Aaron shows an example, with each setTimeout taking an anonymous function. He discusses the timer stack and when they are executed.
Demo: Asynchronous Execution Pattern – a function named buffer which takes items, interFn and callback as arguments
Recursive setTimeout Pattern is a better solution than setinterval if we have a function that is waiting for an AJAX response
Demo: Recursive setTimeout Pattern shows why using setInterval can lead to inconsistent ordering, and the benefit of refactoring to the recursive setTimeout Pattern. This pattern is a lot more efficient.
For a recent discussion on timer reliability see on the nature of timers on getify’s blog
Asynchronous Module Definitions
The latest CommonJS AMD spec is found here on Github.
Pub/Sub Design Pattern
Aaron begins with a shopping cart example, and explains that the code is violating the single responsibility principle.
He also explains that the pub/sub pattern is similar to doing event driven development in .NET.
At the end of this module, Aaron shows how to extend the Pub/Sub library to be more like a message bus.
This begins with an example of creating a simple promise API, and then shows how it works using jQuery.
To learn about promises in ES2015 see the promises chapter in exploring JS