Factory functions in JavaScript
Factories are simply functions that create objects. You can use factories instead of classes, and factories are simpler than classes and easier to reason about.
References
- https://medium.com/humans-create-software/factory-functions-in-javascript-video-d38e49802555
- https://medium.com/javascript-scene/javascript-factory-functions-vs-constructor-functions-vs-classes-2f22ceddf33e
- https://www.sitepoint.com/factory-functions-javascript/
ES6 Destructuring
Destructuring assignment allows you to assign the properties of an array or object to variables using syntax that looks similar to array or object literals. This syntax can be extremely terse, while still exhibiting more clarity than the traditional property access. This new syntax was introduced in ECMAScript 2015 (ES6).
// Array destructuring
let arr = [1, 2, 3];
let [a, b, c] = arr;
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
// Object destructuring
let obj = {
x: 1,
y: 2
}
let {x, y} = obj;
console.log(x); //1
console.log(y); //2
References
ES6 Rest parameters
The rest parameter syntax allows us to represent an indefinite number of arguments as an array. If you prefix a parameter name with the rest operator (…), the parameter receives all remaining parameters via an Array. Only the last parameter can be a “rest parameter”. This syntax was introduced in ECMAScript 2015(commonly called as ES6).
function foo(a, b, ...moreArgs) {
console.log(a); //1
console.log(b); //2
console.log(moreArgs); //[3, 4, 5, 6, 7]
}
foo(1, 2, 3, 4, 5, 6, 7);
References
ES6 modules
ES6 is the first time JavaScript has built in modules.
The goal of ES6 modules was to create a format that both users of CommonJS and of AMD are happy with. Its syntax is even more compact than CommonJS. Their structure can be statically analyzed. Their support of cyclic dependencies is better than CommonJS. Similar to AMD, they have direct support for asynchronous loading and configurable module loading.
Use of native JavaScript modules is dependent on the import
and export
statements.
// file name "sum.js"
function sum(a, b) {
return a+b;
}
export { sum };
// file name "main.js"
import { sum } from "./sum.js";
let result = sum(1,2);
console.log(result); //3
<script type="module" src="main.js"></script>
To get modules to work correctly in a browser, you need to make sure that your server is serving them with a Content-Type
header that contains a JavaScript
MIME type such as text/javascript
References
What is npm-shrinkwrap ?
npm-shrinkwrap command locks down the versions of a package’s dependencies so that you can control exactly which versions of each dependency will be used when your package is installed.
References
- https://docs.npmjs.com/cli/shrinkwrap