const myModule = (function() { const privateVariable = 'Hello World'; function privateMethod() { console.log(privateVariable); } return { publicMethod: function() { privateMethod(); } } })(); myModule.publicMethod();
myModule
constant. Due to the fact that there is a closure, the returned object has access to the functions and variables declared inside the IIFE, even after the completion of the IIFE.
myModule
constant.
myModule
will look like this:
const myModule = { publicMethod: function() { privateMethod(); }};
publicMethod()
, which, in turn, will call the private method privateMethod()
. For example:
// 'Hello World' module.publicMethod();
const myRevealingModule = (function() { let privateVar = 'Peter'; const publicVar = 'Hello World'; function privateFunction() { console.log('Name: '+ privateVar); } function publicSetName(name) { privateVar = name; } function publicGetName() { privateFunction(); } /** , */ return { setName: publicSetName, greeting: publicVar, getName: publicGetName }; })(); myRevealingModule.setName('Mark'); // Name: Mark myRevealingModule.getName();
myRevealingModule
looks like this:
const myRevealingModule = { setName: publicSetName, greeting: publicVar, getName: publicGetName };
myRevealingModule.setName('Mark')
method, which is a reference to the internal publicSetName
function. The myRevealingModule.getName()
method refers to the internal function publicGetName
. For example:
myRevealingModule.setName('Mark'); // Name: Mark myRevealingModule.getName();
export
keyword. The code inside the module is always executed in strict mode.
export
keyword before declaring a function or variable. For example:
// utils.js export const greeting = 'Hello World'; export function sum(num1, num2) { console.log('Sum:', num1, num2); return num1 + num2; } export function subtract(num1, num2) { console.log('Subtract:', num1, num2); return num1 - num2; } // - function privateLog() { console.log('Private Function'); }
export
keyword to the end of the code listing the names of the functions and variables to be exported. For example:
// utils.js function multiply(num1, num2) { console.log('Multiply:', num1, num2); return num1 * num2; } function divide(num1, num2) { console.log('Divide:', num1, num2); return num1 / num2; } // function privateLog() { console.log('Private Function'); } export {multiply, divide};
import
keyword:
// main.js // import { sum, multiply } from './utils.js'; console.log(sum(3, 7)); console.log(multiply(3, 7));
// main.js // , import * as utils from './utils.js'; console.log(utils.sum(3, 7)); console.log(utils.multiply(3, 7));
// utils.js function sum(num1, num2) { console.log('Sum:', num1, num2); return num1 + num2; } function multiply(num1, num2) { console.log('Multiply:', num1, num2); return num1 * num2; } export {sum as add, multiply};
// main.js import { add, multiply as mult } from './utils.js'; console.log(add(3, 7)); console.log(mult(3, 7));
const user = { name: 'Peter', age: 25, job: 'Teacher', greet: function() { console.log('Hello!'); } };
user
variable, we get a link to the same object.
let instance = null; function User(name, age) { if(instance) { return instance; } instance = this; this.name = name; this.age = age; return instance; } const user1 = new User('Peter', 25); const user2 = new User('Mark', 24); // true console.log(user1 === user2);
instance
object exists. If the corresponding variable is not initialized, this
written in the instance
. If the variable already has an object reference, the constructor simply returns an instance
, that is, a reference to an already existing object.
const singleton = (function() { let instance; function User(name, age) { this.name = name; this.age = age; } return { getInstance: function(name, age) { if(!instance) { instance = new User(name, age); } return instance; } } })(); const user1 = singleton.getInstance('Peter', 24); const user2 = singleton.getInstance('Mark', 26); // prints true console.log(user1 === user2);
user
instance by calling the singleton.getInstance()
method. If an instance of the object already exists, then this method will simply return it. If there is no such object yet, the method creates its new instance by calling the User
constructor function.
class Car{ constructor(options) { this.doors = options.doors || 4; this.state = options.state || 'brand new'; this.color = options.color || 'white'; } } class Truck { constructor(options) { this.doors = options.doors || 4; this.state = options.state || 'used'; this.color = options.color || 'black'; } } class VehicleFactory { createVehicle(options) { if(options.vehicleType === 'car') { return new Car(options); } else if(options.vehicleType === 'truck') { return new Truck(options); } } }
Car
and Truck
, which provide for the use of certain standard values. They are used to create car
and truck
objects. Also, the VehicleFactory
class is declared here, which is used to create new objects based on the analysis of the vehicleType
property vehicleType
to the corresponding method of the object returned by it in the object with the options
parameters. Here's how to work with all this:
const factory = new VehicleFactory(); const car = factory.createVehicle({ vehicleType: 'car', doors: 4, color: 'silver', state: 'Brand New' }); const truck= factory.createVehicle({ vehicleType: 'truck', doors: 2, color: 'white', state: 'used' }); // Car {doors: 4, state: "Brand New", color: "silver"} console.log(car); // Truck {doors: 2, state: "used", color: "white"} console.log(truck);
factory
object of class VehicleFactory
created VehicleFactory
. After that, you can create objects of the Car
or Truck
classes by calling the factory.createVehicle()
method and passing it the options
object with the vehicleType
property set to the value of car
or truck
.
function Car(name) { this.name = name; // this.color = 'White'; } // , const tesla= new Car('Tesla Model 3'); // - tesla.setColor = function(color) { this.color = color; } tesla.setPrice = function(price) { this.price = price; } tesla.setColor('black'); tesla.setPrice(49000); // black console.log(tesla.color);
class Car() { } class CarWithAC() { } class CarWithAutoTransmission { } class CarWithPowerLocks { } class CarWithACandPowerLocks { }
Car
, which describes, say, a car in the base configuration, the cost of which is expressed in some fixed amount. After that, the standard object created on the basis of this class can be extended using decorator functions. A standard “car” processed by such a function receives new possibilities, which, moreover, affects its price. For example, this scheme can be implemented as:
class Car { constructor() { // this.cost = function() { return 20000; } } } // - function carWithAC(car) { car.hasAC = true; const prevCost = car.cost(); car.cost = function() { return prevCost + 500; } } // - function carWithAutoTransmission(car) { car.hasAutoTransmission = true; const prevCost = car.cost(); car.cost = function() { return prevCost + 2000; } } // - function carWithPowerLocks(car) { car.hasPowerLocks = true; const prevCost = car.cost(); car.cost = function() { return prevCost + 500; } }
Car
, used to create objects representing standard cars. Then we create several decorator functions that allow us to extend the objects of the base class Car
additional properties. These functions take the corresponding objects as parameters. After that, we add a new property to the object, indicating what new feature the car will be equipped with, and redefine the cost
function of the object, which now returns the new value of the car. As a result, in order to “equip” a standard-configuration car with something new, we can use the following design:
const car = new Car(); console.log(car.cost()); carWithAC(car); carWithAutoTransmission(car); carWithPowerLocks(car);
// console.log(car.cost());
Source: https://habr.com/ru/post/427293/