* All implementations assume `console.log` is a static function with side-effect. * All implementations assume `HELLO_WORLD` is a constant and does not need to be abstracted and does not need to be passed in as an argument. ## Declarative ## ```jsx const HelloWorld = (
{HELLO_WORLD}
); // usage React.render(HelloWorld, doment.body) ``` ## Imperative ## ### Procedural ### #### Direct invocation #### ```js console.log(HELLO_WORLD); // usage // none - procedural code ``` #### Subroutine #### ```js function printHelloWorld() { console.log(HELLO_WORLD); } // usage printHelloWorld(); ``` ### Static-typed object-oriented ### #### Static #### ##### Static method ##### ```js class HelloWorld { static print() { console.log(HELLO_WORLD); } } // usage HelloWorld.print(); ``` ##### Static invocation from method ##### ```js class HelloWorld { print() { console.log(HELLO_WORLD); } } // usage new HelloWorld().print() ``` #### Injection-based #### ##### constructor injection ##### ```js class HelloWorld { constructor(console) { this.console = console; } print() { this.console.log(HELLO_WORLD); } } // usage new HelloWorld(console).print(); ``` ##### Parameter Injection ##### ```js class HelloWorld { print(console) { console.log(HELLO_WORLD); } } // usage new HelloWorld().print(console); ``` ##### Autowired Injection ##### ```js class HelloWorld { $console; print(console) { this.$console.log(HELLO_WORLD); } } // usage Singleton.get(HelloWorld).print(console); ``` ### Inheritance ### #### Classical Inheritance #### ```js // following code would not work as Console is not publically initializable class HelloWorld extends Console { print() { this.log(HELLO_WORLD); } } // usage new HelloWorld().log(); ``` #### Prototypical inheritance #### ```js let myConsole = Object.create(console); myConsole.print = function () { this.log(HELLO_WORLD); }; // usage myConsole.print(); ``` #### Old-school class pattern #### ```js // following code would not work as Console is not publically initializable function HelloWorld() { } HelloWorld.prototype = new Console(); HelloWorld.prototype.print = function () { this.log(HELLO_WORLD); }; // usage new HelloWorld().log(); ``` ### Dynamic-typed object-oriented ### #### Mixin #### ##### Static invocation ##### ```js const HelloWorldMixin = { printHelloWorld() { console.log(HELLO_WORLD); } }; // usage const obj = Object.assign({}, HelloWorldMixin); obj.printHelloWorld(); ``` ##### Parameter injection ##### ```js const HelloWorldMixin = { printHelloWorld(console) { console.log(HELLO_WORLD); } }; // usage const obj = Object.assign({}, HelloWorldMixin); obj.printHelloWorld(console); ``` #### Monkey-pactching #### ##### Monkey-pactching with mixins ##### ```js const HelloWorldMixin = { printHelloWorld() { console.log(HELLO_WORLD); } }; // usage Object.assign(console, HelloWorldMixin); console.printHelloWorld(); ``` #### Monkey-pactching with method #### ```js console.printHelloWorld = function() { this.log(HELLO_WORLD); } // usage console.printHelloWorld(); ``` ### Factory / Builder ### #### Constructor Injection #### ```js function createHelloWorld(f) { return { print() { f.log(HELLO_WORLD); } } } // usage createHelloWorld(console).print(); ``` #### Parameter Injection #### ```js function createHelloWorld() { return { print(console) { console.log(HELLO_WORLD); } }; } // usage createHelloWorld().print(console); ``` #### IIFE / old-school module pattern #### ```js const HelloWorld = (function createHelloWorld() { return { print(console) { console.log(HELLO_WORLD); } }; })(); // usage HelloWorld.print(console); ``` ## Functional programming ## ### Eager Eval ### #### Side-effect on usage #### ```js // This is a contrived case as IO is all about side-effect function helloWorld() { return HELLO_WORLD; } // usage console.log(helloWorld()); ``` #### Side-effect on binding #### ```js function helloWorld(console) { return console.log(HELLO_WORLD); } // usage helloWorld(console); ``` ### Lazy Eval ### #### early binding #### ```js function helloWorld(f) { return () => f.log(HELLO_WORLD); } // usage helloWorld(console)(); ``` ##### Late Binding ##### ```js function helloWorld() { return (f) => f.log(HELLO_WORLD); } // usage helloWorld()(console); ```