(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 TestPistons() {
this.action = () => log("The test pistons do nothing.");
log("Made some test pistons.");
}
function Engine(pistons) {
this.pistons = pistons;
this.action = () => {
this.pistons.action();
log("The engine goes vroom vroom.");
};
log("Made an engine.");
}
function Car(wheels, engine) {
this.wheels = wheels;
this.engine = engine;
this.action = () => {
this.wheels.action();
this.engine.action();
log("The car drives by.");
}
log("Made a car.");
}
var pistons = new Pistons();
var testPistons = new TestPistons();
var wheels = new Wheels();
var engine = new Engine(pistons);
var testEngine = new Engine(testPistons);
var car = new Car(wheels, engine);
car.action();
testEngine.action();
})();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.