… or how to leverage AMD’s require/define semantics for fine-grained dependency injection.
update: code now available on gist.github.com: https://gist.github.com/4089076
update2: better implementation of calling the service constructor in resolveInstance() to follow operator new semantics.
i’ve also become a big fan of Ninject for its lightweight but very effective dependency injection library for .Net.
this inspired me to implement a dependency injection container using define()/require()-like semantics, including support for circular dependency resolution.
you can download the source files for the ServiceKernel class described in this post, including unit tests, from here: https://gist.github.com/4089076
note: the source code provided has a few dependencies on ECMAScript 5 features, such as Array.isArray, Array.prototype.indexOf, and Function.prototype.bind. You can easily find replacements for these functions should you need to target an older platform.
… or what to do when there’s no DataView.
updated: corrected the output byte order in
there were two main challenges to overcome:
jDataView is a library that has the ability to read 64-bit IEEE values from a buffer, and it has been recently updated to also support writing 64-bit IEEE values.
here is my solution to the problem until DataView becomes widely available.