{"id":2092,"date":"2015-01-28T13:15:07","date_gmt":"2015-01-28T11:15:07","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=2092"},"modified":"2015-01-26T16:03:25","modified_gmt":"2015-01-26T14:03:25","slug":"dependency-injection-explained-via-javascript","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/","title":{"rendered":"Dependency Injection Explained via JavaScript"},"content":{"rendered":"<p>When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the functionality from scratch to better understand the motivation behind the framework and what it may be saving me by using it.<\/p>\n<p><a href=\"http:\/\/angularjs.org\" target=\"_blank\">Angular<\/a> is no exception. There are many tools in the AngularJS toolbox, from data-binding to compiling new HTML tags, but one of my favorites is the built-in dependency injection. You can <a href=\"https:\/\/github.com\/angular\/angular.js\/blob\/master\/src\/auto\/injector.js\" target=\"_blank\">browse Angular\u2019s DI code here<\/a> and read my blog posts about <a href=\"http:\/\/csharperimage.jeremylikness.com\/2014\/01\/understanding-providers-services-and.html\" target=\"_blank\">understanding Providers, Services, and Factories<\/a>. A more advanced version is detailed in <a href=\"http:\/\/csharperimage.jeremylikness.com\/2014\/01\/interception-using-decorator-and-lazy.html\" target=\"_blank\">Interception using Decorator and Lazy Loading with AngularJS<\/a>.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n<em><a href=\"http:\/\/lh6.ggpht.com\/-0traaaSANYY\/U682pdA1MzI\/AAAAAAAABQQ\/GbGMGTTfrjA\/s1600-h\/chimera3sm%25255B9%25255D.jpg\"><img decoding=\"async\" class=\" alignright\" title=\"chimera3sm\" src=\"http:\/\/lh4.ggpht.com\/-10FMqvR6ia8\/U682poi2cQI\/AAAAAAAABQY\/pLj4kmg3hx4\/chimera3sm_thumb%25255B7%25255D.jpg?imgmax=800\" alt=\"\" width=\"242\" height=\"201\" align=\"left\" border=\"0\" \/><\/a> \u201cWell, Dimitri, every search for a hero must begin with something every hero needs, a villain. So in a search for our hero, Bellerophon, we have created a more effective monster: Chimera.\u201d \u2013 <\/em><strong>Dr. Nekhorvich, <em>Mission Impossible II<\/em><\/strong><\/p>\n<p>I don\u2019t believe I created a monster, but in my search to understand JavaScript dependency injection, I did create <a href=\"https:\/\/github.com\/JeremyLikness\/jsInject\" target=\"_blank\">jsInject<\/a>. This library acts as an dependency injection container for JavaScript components that depend on each other. It is based on <em>constructor injection<\/em> which inverts control of dependencies by passing them in through the constructor. If you are looking for dependency injection in JavaScript without taking on a full framework, this is an extremely lightweight solution that should do the trick for you.<\/p>\n<p>For example, if your serviceA depends on dependencyB, you might define it like this to return it from a factory:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction serviceA(dependencyB) {\r\n    return {\r\n      id: dependencyB.getId()\r\n    };\r\n}<\/pre>\n<p>Another approach is to use a constructor function. This is required if you use a class-based approach, as code generated from tools like TypeScript doesn\u2019t lend itself well to the factory pattern.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">function ServiceA(dependencyB) {\r\n    this.id = dependencyB.getId();\r\n}<\/pre>\n<p>Of course there is always the self-invoking function as well:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">var ServiceA = (function() {\r\n    function ServiceA(dependencyB) {\r\n        this.id = dependencyB.getId();\r\n    }\r\n    return ServiceA;\r\n})();<\/pre>\n<p>In all of these cases, the dependency is injected and the purpose of a dependency injection container is to handle that injection for you.<\/p>\n<h2>Why Dependency Injection?<\/h2>\n<p>A logical first question is: why bother? I often hear this from developers and architects who are concerned that dependency injection adds unnecessary overhead and overcomplicates projects. More often than not they have worked on smaller projects with smaller teams and might not have run into the sheer size of project that benefits from dependency injection. I\u2019m used to projects where there are tens of thousands if not hundreds of thousands of lines of client code (yes, I\u2019m talking just the JavaScript part) with hundreds of components that interrelate. Forget module loading, bundling, etc. for a moment (worrying about how the scripts get loaded in the first place), let\u2019s take a look at common problems:<\/p>\n<ul>\n<li>When A depends on B and B depends on C, without dependency injection you create C from B and B from A and then reference A from a dozen different places. What happens when C now requires something, or B requires D in addition to C? Without dependency injection, that is a lot of refactoring and finding places you create those components. With dependency injection, A only has to worry about B. A doesn\u2019t care what B depends on because that is handled by the DI container.<\/li>\n<li>Timing is often an issue. What happens when A depends on B but the script for B is loaded <em>after<\/em> A? Dependency injection removes that concern because you can defer or <em>lazy load<\/em> the dependency when it\u2019s time. In other words, regardless of how A and B are loaded, you only need to wire them up when you first reference A. On large projects when you have 50 JavaScript components referenced from a single page, the last thing you want to have to worry about is including them in the correct order.<\/li>\n<li>Dependency injection is critical for testing. If A directly instantiates B then I\u2019m stuck with the implementation of B that A chooses. If B is injected into A, I can create a \u201cmock B\u201d or a \u201cstub B\u201d for testing purposes. For example, you might have a component that depends on a web service. With dependency injection, you can create an implementation that uses hard-coded JSON for testing purposes and then wire in the real component that makes the service call at runtime.<\/li>\n<li>Single Responsibility \u2013 Dependency Injection is actually one of the <a href=\"http:\/\/csharperimage.jeremylikness.com\/2009\/05\/solid-and-dry.html\" target=\"_blank\">SOLID principles<\/a>. It helps to encourage the notion of designing components with a Single Responsibility. You want to be able to focus on one thing in a component an should only have to change it for one reason. This is like building padding around your component, insulting the rest of the system. If the component is responsible for boiling water and mixing shakes, anytime you change it you have to test both scenarios. Having a single responsibility means you can change how you boil water without worrying about making shakes because another component is responsible. That makes it easier to maintain the code.<\/li>\n<li>Psst \u2026 managers. Here\u2019s something else to think about. Forget the technology, let\u2019s talk about teams. When you have a lot of cooks in the kitchen, it is easy for them to bump elbows and step on each other\u2019s toes. Having nice, isolated components means more team members can work in parallel without having to worry about what the other person is doing. I\u2019ve witnessed this on many projects \u2013 again, if you have one component boil water and mix shakes, only one person can apply changes at a time. If you have two separate components, you can improve both scenarios at the same time.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/lh6.ggpht.com\/-k3QreU6Rp88\/U682qAxOuJI\/AAAAAAAABQg\/N26SK--hbyc\/s1600-h\/hero%25255B4%25255D.jpg\"><img decoding=\"async\" title=\"hero\" src=\"http:\/\/lh3.ggpht.com\/-ZhKVA__Alwc\/U682qvtt5iI\/AAAAAAAABQo\/BB00ulgS5iM\/hero_thumb%25255B2%25255D.jpg?imgmax=800\" alt=\"\" width=\"242\" height=\"166\" align=\"right\" border=\"0\" \/><\/a> I believe there are many benefits and that DI itself is only as complicated as you make it. In my experience, it simplifies things on larger projects.<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n<h2>Dependency Injection to the Rescue<\/h2>\n<p>The most basic DI solutions provide at least two features: the ability to register a component, and the ability to retrieve an instance of a component. Advanced solutions will allow you to intercept requests, override methods on the fly, register multiple components that satisfy a given interface and even manage the lifetime of a component (whether you get a new instance or the same one each time). The registration is key because that tells the container what you expect to retrieve, and somehow the container must also understand what dependencies to provide.<\/p>\n<p>A naive implementation will use the constructor parameters to determine dependencies. I call this naive because naming a parameter doesn\u2019t imply the dependent component has the same name, and when you minify or uglify your JavaScript code you lose information. A more common approach is to annotate the component somehow. I really like the options that Angular provides for annotations. When you register a component, you give it a name that doesn\u2019t necessarily have to match the name of the constructor function or the function being called as a factory. When you annotate a component, you simply provide a list of names of dependencies. There are two ways to do this. You can either mark these up on the component itself by exposing a property as an array, or you can pass these in to the container when you register it.<\/p>\n<h2>Registering the Component<\/h2>\n<p>Let\u2019s register a component. For jsInject I decided to mirror Angular\u2019s approach and allow you to either pass dependencies at registration time, or use a static property. Here is an example of providing the information at registration time:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">var fn = (function() {\r\n   function Fn(echo) {\r\n      this.echo = echo;\r\n      this.test = function() {\r\n         return echo.echo(expected);\r\n      };\r\n    }\r\n    return Fn;\r\n})();\r\n$jsInject.register('1', &#x5B;'echoFn', fn]);<\/pre>\n<p>In this case the dependency passed in as \u201cecho\u201d to the constructor is registered as \u201cechoFn\u201d to the container, and the function we are registering called fn is named \u201c1\u201d in the container. The registration expects an array. The last member should always be the component we are registering (whether it is a constructor function or factory) and the members before it are the names of the dependencies in the order they will be passed in.<\/p>\n<p>The other approach is to annotate the component. Here is an example of using the annotation approach. This service exposes a hard-coded static property called $$deps to list the dependency names:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">function ServiceB(serviceA) {\r\n    this.id = serviceA.id + 'b';\r\n}\r\nServiceB.$$deps = &#x5B;'ServiceA'];<\/pre>\n<p>It is registered like this (notice there are no \u201cannotations\u201d added to the registration array, just the constructor function itself):<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">$jsInject.register('ServiceA', &#x5B;ServiceA]);<\/pre>\n<p>jsInject creates a method for instantiating the component but does not try to invoke it right away. It is a lazy-loading function. The first time the component is needed, it will instantiate it, then it will return the same copy for future requests. Here is the general pattern \u2013 we\u2019ll expand the magic part, but note how once it\u2019s done, it won\u2019t go through that work again because the function replaces itself with a new one that simply returns the instantiated component.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">var _this = this;\r\nthis.container&#x5B;name] = function (level) {\r\n    var result = {}; \/\/ magic goes here\r\n    _this.container&#x5B;name] = function () {\r\n        return result;\r\n    };\r\n    return result;\r\n};<\/pre>\n<p>So how exactly does it wire up the dependencies?<\/p>\n<h2>Retrieving the Component<\/h2>\n<p>The magic is in walking through the dependency chain. The algorithm itself is fairly simple. You basically iterate the list of annotations, grab them from the container recursively, and shove them into a list of arguments that you\u2019ll pass to the component\u2019s constructor. If an annotation has been instantiated, it is returned, otherwise it is also scanned for its own annotations and so forth. The trick is understanding the format of what was passed in and generically instantiating it with a dynamic constructor list.<\/p>\n<p>The level keeps track of recursion to avoid infinite loops. You can tweak this as needed but I\u2019ve seldom seen well-written systems go more than a few levels deep (for example, a controller may depend on a service that depends on other services). The dynamic nature of JavaScript makes it easier for us to build a component on the fly. First, we create an empty template:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">Template = function () {}<\/pre>\n<p>Next, we get the component itself (which is a function, but it might be a constructor function, a factory, or a self-invoking function):<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">fn = annotatedArray&#x5B;annotatedArray.length - 1],<\/pre>\n<p>Then we grab the annotations for the component. Remember, the last element of the array is the component itself. If the array has more than one element, we assume the previous elements are annotations. Otherwise, we check the component itself for the $$deps property, and failing that we assume it has no dependencies.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">deps = annotatedArray.length === 1 ? (annotatedArray&#x5B;0].$$deps || &#x5B;]) :\r\n    annotatedArray.slice(0, annotatedArray.length - 1),<\/pre>\n<p>Now for the magic, we copy the prototype of the supplied component to the prototype of our template:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">Template.prototype = fn.prototype;<\/pre>\n<p>Then we create an instance of the template:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">instance = new Template();<\/pre>\n<p>Finally, we call a recursive invoke function to push in dependencies. If there are none, we use the instance we created, otherwise we use the fully wired result from the recursive call.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">injected = _this.invoke(fn, deps, instance, lvl + 1);\r\nresult = injected || instance;<\/pre>\n<p>The recursive call simply checks the recursion level to make sure we haven\u2019t gone too far, then iterates through the dependencies. Each dependency is pushed into an arguments list (as an instance that is also recursively retrieved from the container itself). We use the function to apply to the template we created with the arguments we\u2019ve pushed.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">for (; i &lt; deps.length; i += 1) {\r\n    args.push(this.get(deps&#x5B;i], lvl + 1));\r\n}\r\nreturn fn.apply(instance, args);<\/pre>\n<p>Voila! Now we are able to retrieve a component with its dependencies. Here is an example of wiring up multiple components, some of them by annotating during registration and others annotated using the $$deps:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">function main () {\r\n    var ioc = new $$jsInject();\r\n    ioc.register('courseMap', &#x5B;CourseMap]);\r\n    ioc.register('instructors', &#x5B;'log', 'ch', Instructors]);    \r\n    ioc.register('courses', &#x5B;'log', 'ch', Courses]);\r\n    ioc.register('log', &#x5B;Log]);\r\n    ioc.register('ch', &#x5B;CollectionHelper]);\r\n    ioc.get('log').log(ioc.get('courseMap').getCourses());         \r\n}<\/pre>\n<p>You can see a full working example <a href=\"http:\/\/jsfiddle.net\/jeremylikness\/z9DP8\/\" target=\"_blank\">with this fiddle<\/a>. Be sure to open your console log, that is where you can verify the call to the course map returns an expanded instructor and course but all dependent components are only created once even though they were registered in a different order.<\/p>\n<h2>Improvements<\/h2>\n<p>You can certainly improve the implementation (and I do accept pull requests). As an exercise, for example, how would you modify it so you could store constant values and not just instances of objects? In other words, what if I want to register the text \u201cCopyright 2014\u201d in a value called \u201cCopyright\u201d? Also, the current implementation for registration doesn\u2019t implement chaining. Instead of ioc.register(x); ioc.register(y) it would be far more convenient to chain like this: ioc.register(x).register(y) \u2026 do you know how to fix it so that\u2019s possible? What about a modification that allows you to determine whether you get the singleton, cached instance vs. force a \u201cfresh\u201d instance when you request it? (Hint: take a look at my <a href=\"https:\/\/portableioc.codeplex.com\/\" target=\"_blank\">Portable IOC<\/a> project that supports this in C#).<\/p>\n<h2>Wrapping Up<\/h2>\n<p>As you can see, dependency injection itself is fairly straightforward and is designed to simplify your solution, not overly complicate it. Angular adds some extra caveats and shortcuts to their solution but in essence it behaves very similar to the example I\u2019ve provided here. In fact, I hope by walking through this you can now go back to the Angular source and better understand what is happening in the $inject implementation. If you are able to use the <a href=\"https:\/\/github.com\/JeremyLikness\/jsInject\" target=\"_blank\">jsInject<\/a> component in your projects that\u2019s an added bonus, but otherwise I hope you walk away feeling much better about using dependency injection and specifically how it can be implemented in JavaScript.<\/p>\n<p>* Chimera image credit goes to <a href=\"http:\/\/akhsmythology.wikispaces.com\/Typhon-+Bannor+Patterson\" target=\"_blank\">AKHSMythology<\/a> under the <a href=\"http:\/\/www.creativecommons.org\/licenses\/by-sa\/3.0\" target=\"_blank\">Creative Commons Attribution Share-Alike 3.0 License<\/a><br \/>\n* Hero image credit Office ClipArt<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/csharperimage.jeremylikness.com\/2014\/06\/dependency-injection-explained-via.html\">Dependency Injection Explained via JavaScript<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/wcg\/\">WCG partner<\/a> Jeremy Likness at the <a href=\"http:\/\/csharperimage.jeremylikness.com\/\">C#er : IMage<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the functionality from scratch to better understand the motivation behind the framework and what it may be saving me by using it. Angular is no exception. There are many &hellip;<\/p>\n","protected":false},"author":40,"featured_media":920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[73],"class_list":["post-2092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-dependency-injection"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Dependency Injection Explained via JavaScript - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dependency Injection Explained via JavaScript - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-28T11:15:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jeremy Likness\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/jeremylikness\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeremy Likness\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\"},\"author\":{\"name\":\"Jeremy Likness\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/2cd31c082e3e1cc5f595ba18a6e03a43\"},\"headline\":\"Dependency Injection Explained via JavaScript\",\"datePublished\":\"2015-01-28T11:15:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\"},\"wordCount\":2384,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"keywords\":[\"Dependency Injection\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\",\"name\":\"Dependency Injection Explained via JavaScript - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"datePublished\":\"2015-01-28T11:15:07+00:00\",\"description\":\"When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Dependency Injection Explained via JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/2cd31c082e3e1cc5f595ba18a6e03a43\",\"name\":\"Jeremy Likness\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b4d8ee78925818e3f7a4d0d82df06d34a2b76aa4d260a1f4b67dc0553efbf1b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b4d8ee78925818e3f7a4d0d82df06d34a2b76aa4d260a1f4b67dc0553efbf1b6?s=96&d=mm&r=g\",\"caption\":\"Jeremy Likness\"},\"description\":\"Jeremy Likness is a principal architect at iVision, Inc. He has been building enterprise applications using the Microsoft stack for 20 years with a focus on web-based solutions for the past 15. A prolific author and speaker, Jeremy's mission is to empower developers to create success in their careers through learning and growth.\",\"sameAs\":[\"http:\/\/csharperimage.jeremylikness.com\/\",\"http:\/\/linkedin.com\/in\/jeremylikness\",\"https:\/\/x.com\/https:\/\/twitter.com\/jeremylikness\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/jeremy-likness\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dependency Injection Explained via JavaScript - Web Code Geeks - 2026","description":"When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Dependency Injection Explained via JavaScript - Web Code Geeks - 2026","og_description":"When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2015-01-28T11:15:07+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","type":"image\/jpeg"}],"author":"Jeremy Likness","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/jeremylikness","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Jeremy Likness","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/"},"author":{"name":"Jeremy Likness","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/2cd31c082e3e1cc5f595ba18a6e03a43"},"headline":"Dependency Injection Explained via JavaScript","datePublished":"2015-01-28T11:15:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/"},"wordCount":2384,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","keywords":["Dependency Injection"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/","name":"Dependency Injection Explained via JavaScript - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","datePublished":"2015-01-28T11:15:07+00:00","description":"When learning a new framework I often find it is useful to examine the source, use the framework, then go into a separate project and build the","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/dependency-injection-explained-via-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Dependency Injection Explained via JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/2cd31c082e3e1cc5f595ba18a6e03a43","name":"Jeremy Likness","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b4d8ee78925818e3f7a4d0d82df06d34a2b76aa4d260a1f4b67dc0553efbf1b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b4d8ee78925818e3f7a4d0d82df06d34a2b76aa4d260a1f4b67dc0553efbf1b6?s=96&d=mm&r=g","caption":"Jeremy Likness"},"description":"Jeremy Likness is a principal architect at iVision, Inc. He has been building enterprise applications using the Microsoft stack for 20 years with a focus on web-based solutions for the past 15. A prolific author and speaker, Jeremy's mission is to empower developers to create success in their careers through learning and growth.","sameAs":["http:\/\/csharperimage.jeremylikness.com\/","http:\/\/linkedin.com\/in\/jeremylikness","https:\/\/x.com\/https:\/\/twitter.com\/jeremylikness"],"url":"https:\/\/www.webcodegeeks.com\/author\/jeremy-likness\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/2092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=2092"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/920"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}