A quick explanation of how the AngularJS dependency injector works, and how you could write your own simplified JavaScript dependency injection library.
Dependency injection facilitates better testing by allowing us to mock dependencies in testing environments so that we only test one thing at a time. It also enables us to write more maintainable code by decoupling our objects from their implementations.
With dependency injection, your dependencies are given to your object instead of your object creating or explicitly referencing them. This means the dependency injector can provide a different dependency based on the context of the situation. For example, in your tests it might pass a fake version of your services API that doesn't make requests but returns static objects instead, while in production it provides the actual services API.
Another example could be to pass ZeptoJS to your view objects when the device is running Webkit instead of jQuery to improve performance.
Your new post is loading...