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 thus can be inlined. ## Declarative ## ```jsx const HelloWorld = (
Hello world!!!
); // usage React.render(HelloWorld, doment.body) ``` ## Imperative ## ### Procedural ### #### Direct invocation #### ``` console.log('Hello world!!!'); // usage // none - procedural code ``` #### Subroutine #### ``` function printHelloWorld() { console.log('Hello world!!!'); } // usage printHelloWorld(); ``` ### Static-typed object-oriented ### #### Static #### ##### Static method ##### ``` class HelloWorld { static print() { console.log('Hello world!!!'); } } // usage HelloWorld.print(); ``` ##### Static invocation from method ##### ``` class HelloWorld { print() { console.log('Hello world!!!'); } } // usage new HelloWorld().print() ``` #### Injection-based #### ##### constructor injection ##### ``` class HelloWorld { constructor(console) { this.console = console; } print() { this.console.log('Hello world!!!'); } } // usage new HelloWorld(console).print(); ``` ##### Parameter Injection ##### ``` class HelloWorld { print(console) { console.log('Hello world!!!'); } } // usage new HelloWorld().print(console); ``` ##### Autowired Injection ##### ``` class HelloWorld { $console; print(console) { this.$console.log('Hello world!!!'); } } // usage Singleton.get(HelloWorld).print(console); ``` ### Inheritance ### #### Classical Inheritance #### ``` // 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 #### ``` let myConsole = Object.create(console); myConsole.print = function () { this.log('Hello world!!!'); }; // usage myConsole.print(); ``` #### Old-school class pattern #### ``` // 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 ##### ``` const HelloWorldMixin = { printHelloWorld() { console.log('Hello World!!!'); } }; // usage const obj = Object.assign({}, HelloWorldMixin); obj.printHelloWorld(); ``` ##### Parameter injection ##### ``` const HelloWorldMixin = { printHelloWorld(console) { console.log('Hello World!!!'); } }; // usage const obj = Object.assign({}, HelloWorldMixin); obj.printHelloWorld(console); ``` ##### Target Mixin ##### ``` const HelloWorldMixin = { printHelloWorld() { console.log('Hello World!!!'); } }; // usage Object.assign(console, HelloWorldMixin); console.printHelloWorld(); ``` #### Monkey pactching #### ``` console.printHelloWorld = function() { this.log('Hello world!!!'); } // usage console.printHelloWorld(); ``` ### Factory / Builder ### #### Constructor Injection #### ``` function createHelloWorld(f) { return { print() { f.log('Hello world!!!'); } } } // usage createHelloWorld(console).print(); ``` #### Parameter Injection #### ``` function createHelloWorld() { return { print(console) { console.log('Hello world!!!'); } }; } // usage createHelloWorld().print(console); ``` #### IIFE / old-school module pattern #### ``` const HelloWorld = (function createHelloWorld() { return { print(console) { console.log('Hello world!!!'); } }; })(); // usage HelloWorld.print(console); ``` ## Functional programming ## ### Eager Eval ### #### Side-effect on binding #### ``` // functional - eager eval - no side-effect function helloWorld(console) { return console.log('Hello world!!!'); } // usage helloWorld(console); ``` #### Side-effect on usage #### ``` no side-effect function helloWorld() { return 'Hello World!!!'; } // usage console.log(helloWorld()); ``` ### Lazy Eval ### #### early binding #### ``` function helloWorld(f) { return () => f.log('Hello world!!!'); } // usage helloWorld(console)(); ``` ##### Late Binding ##### ``` function helloWorld() { return (f) => f.log('Hello world!!!'); } // usage helloWorld()(console); ```