JavaScript without Dependency Injection

Example app for illustrating DI

by Jeremy Likness

HTML

<div id="output">
</div>

JavaScript

(function () {

	const output = document.getElementById("output");

	const log = msg => {
  	var p = document.createElement("p");
    p.innerText = msg;
    output.appendChild(p);
  };

	function Wheels() {
  	this.action = () => log("The wheels go 'round and 'round.");
    log("Made some wheels.");
  }
  
  function Pistons() {
  	this.action = () => log("The pistons fire up and down.");
    log("Made some pistons.");
  }
  
  function Engine() {
  	this.pistons = new Pistons();
    this.action = () => {
    	this.pistons.action();
      log("The engine goes vroom vroom.");
    };
    log("Made an engine.");
  }
  
  function Car() {
  	this.wheels = new Wheels();
    this.engine = new Engine();
    this.action = () => {
    	this.wheels.action();
      this.engine.action();
      log("The car drives by.");
    };
    log("Made a car.");
  }
  
  var car = new Car();
  car.action();


})();