Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Modules are an integral piece of any robust
- * application's architecture and typically
- * help in keeping the unit of code for a
- * project cleanly separated and organized.
- *
- * There are several option for implementing modules. These include:
- * - Object literal notation
- * - The modules Pattern
- * - AMD modules
- * - CommonJS module
- * - ECMAScript Harmony modules
- */
- // Object literal
- const newObject = {
- variableKey: variableValue,
- functionKey: function () {
- // …
- }
- };
- // ----------------------------------------------------------------------------
- // Let's begin looking at an implementation of the Module pattern
- // by created a module that is self-contained.
- // Modules Pattern
- const testModule = (function () {
- let counter = 0;
- return {
- incrementCounter: function () {
- return ++counter;
- },
- resetCounter: function () {
- counter = 0;
- }
- };
- })();
- // usage:
- testModule.incrementCounter();
- testModule.resetCounter();
- // ----------------------------------------------------------------------------
- // One thing that the revealing module can do is avoiding
- // repeat the name of the main object when we want to call
- // one public method from another or access public variables.
- // Revealing Module Pattern
- const myRevealingModule = (function () {
- let privateVariable = 'not okay',
- publicVariable = 'okay';
- function privateFun() {
- return privateVariable;
- }
- function publicSetName(strName) {
- privateVariable = strName;
- }
- function publicGetName() {
- privateFun();
- }
- return {
- setName: publicSetName,
- message: publicVariable,
- getName: publicGetName
- };
- })();
- // usage:
- myRevealingModule.setName('Marvin King');
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- /*
- * The Singleton pattern is thus known because it restricts instantiation
- * of a class to single object. Singletons differ from static classes as we
- * can delay their initialization. generally because they require some information
- * that may not be available during initialization time. For code that is unaware
- * of a previous reference to them, they do not provide a method for easy retrieval.
- * Let's have a look of the structure of singleton:
- */
- const singletonPattern = (function() {
- let instance;
- function init() {
- // singleton
- function privateMethod() {
- console.log('privateMethod');
- }
- let privateVariable = 'this is private variable';
- let privateRandomNumber = Math.random();
- return {
- publicMethod: function() {
- console.log('publicMethod');
- },
- publicProperty: 'this is public property',
- getRandomNumber: function() {
- return privateRandomNumber;
- }
- };
- }
- return {
- // get the singleton instance if one exists
- // or create if it doesn't
- getInstance: function() {
- if (!instance) {
- instance = init();
- }
- return instance;
- }
- };
- })();
- // usage:
- const single = singletonPattern.getInstance();
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- /*
- * One of the benefits of using the Prototype pattern is that we've working with
- * the prototype strengths JavaScript has to offer natively rather than attempting
- * to imitate features of other languages. let's look at the pattern example.
- */
- const myCar = {
- name: 'bmw',
- drive: function () {
- console.log('I am driving!');
- },
- panic: function () {
- console.log('wait, how do you stop this thing?');
- }
- };
- // usage:
- const yourCar = Object.create(myCar);
- console.log(yourCar.name); // 'bmw'
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- // ----------------------------------------------------------------------------
- /*
- * Mixins are classes that offer functionality that
- * can be easily inherited by a sub-class or group of
- * sub-classes for the purpose of the function reuse.
- */
- function Person(firstName, lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.gender = 'male';
- this.introduce = function introduce() {
- console.log(this.firstName + ' ' + this.lastName);
- }
- };
- const reporter = new Person('Clark', 'Kent');
- function SuperHero(firstName, lastName, powers) {
- Person.call(this, firstName, lastName);
- this.powers = powers;
- };
- // SuperHero will inherit Person
- SuperHero.prototype = Object.create(Person.prototype);
- SuperHero.prototype.constructor = SuperHero;
- const superman = new SuperHero('Clark', 'Kent', ['flight', 'heat-vision']);
- console.log(reporter);
- console.log(superman); // output personal attributes as well as power
Advertisement
Add Comment
Please, Sign In to add comment