Last active
August 29, 2015 14:18
-
-
Save groundwater/5b3a90337d19f9bb7a0a to your computer and use it in GitHub Desktop.
JavaScript Modules and Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Router} from './myRouter' | |
import {Component} from './myComponent' with {Router} // inject Router into imported module | |
const component = new Component() | |
component.doSomething() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Router} // injected by parent | |
export class Component { | |
constructor() { | |
this.router = new Router() // this is injected by the parent module | |
} | |
doSomething() { | |
this.router.doSomething() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// myComponent.js | |
module.exports = function(Router) { | |
function Component() { | |
this.router = new Router() | |
} | |
Component.prototype.doSomething = function(){ | |
this.router.doSomething() | |
} | |
return {Component: Component} | |
} | |
// index.js | |
var Router = require('./myRouter').Router | |
var Component = require('./myComponent')(Router).Component // inject Router into imported module | |
const component = new Component() | |
component.doSomething() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment