Posts

Showing posts from 2017
Factory functions New keyword creates a new object sets the this context to the new object good examples at  https://stackoverflow.com/ questions/1646698/what-is-the- new-keyword-in-javascript    Factory function any function that returns a new object (just done manually) createUser : function(username) {       return {           name: username       } }
https://simonsmith.io/dipping-a-toe-into-functional-js-with-lodash-fp/ Great blog on Functional programming. For me does an excellent job of highlighting the difference between pure functions and bonified functional programming. lodash fp is a wrapper around lodash to provide immutable, auto curried, iteratee first data last functions. Currying seems to make sense here, just functions that return a function. The data param is often omitted at the end to make the function more re-usable. Currying, just keeps returning a function until it has all the arguments. This is why it makes sense to have the data arg last so it can be provided last. I still can't see the functional programming being realistically easier to use but pure functions definitely make a lot of sense.

Stubbing constructors in JS

If a  constructor is exported directly from a module, it cannot be stubbed in any module that requires it directly. For instance ========= MyConstructor.js ========= module.exports = function(){    console.log('MyConstructor called');  }; and a SUT ========= MyConsumer.js ========= var MyConstructor = require('./MyConstructor'); function MyConsumer(){     this.useful = new MyConstructor(); }; module.exports = MyConsumer; You cannot stub the constructor. From my understanding the ECMAScript spec doesnt provide hooks for the new() operator.  Lets look at some workarounds. 1) Use an init method. This involves using a constructor which calls an init() method. The init() method can then be stubbed out in the usual way.  2) Stub the constructor using rewire // in "dependency.js" module . exports = function ( args ) { return new Dependency ( args ); } // in "foo.js" var dependency = require ( &#