JavaScript
References
Keep JavaScript out of the global scope
Structuring vanilla JS projects
Combine and minify JS project files
- Gulp: https://gulpjs.com/
- Gulp Project Boilerplate: https://github.com/cferdinandi/gulp-boilerplate
Code splitting vanilla JS
Load a JS script conditionally or asynchronously
- Use loadJS to conditionally load a JS file
- https://github.com/filamentgroup/loadJS
Helpful JS blog
Service Workers
Object Destructuring
https://codeburst.io/es6-destructuring-the-complete-guide-7f842d08b98f
const student = {
firstname: 'Glad',
lastname: 'Chinda',
country: 'Nigeria'
};
// Object Destructuring
const { firstname, lastname, country } = student;
console.log(firstname, lastname, country); // Glad Chinda Nigeria
// Initialize local variables
let country = 'Canada';
let firstname = 'John';
let lastname = 'Doe';
const student = {
firstname: 'Glad',
lastname: 'Chinda',
country: 'Nigeria'
};
// Reassign firstname and lastname using destructuring
// Enclose in a pair of parentheses, since this is an assignment expression
({ firstname, lastname } = student);
// country remains unchanged (Canada)
console.log(firstname, lastname, country); // Glad Chinda Canada
Type Checking
The array, plain object, date, regex, and null all return object. The only really accurate ones are the string, function, boolean, and undefined.
https://ultimatecourses.com/blog/understanding-javascript-types-and-reliable-type-checking
Axis (micro library with type checking helper functions): https://github.com/toddmotto/axis
Array
Array.isArray();
Object
https://gomakethings.com/true-type-checking-with-vanilla-js/
Object.prototype.toString.call([]); // [object Array]
Object.prototype.toString.call({}); // [object Object]
Object.prototype.toString.call(''); // [object String]
Object.prototype.toString.call(new Date()); // [object Date]
Object.prototype.toString.call(1); // [object Number]
Object.prototype.toString.call(function () {}); // [object Function]
Object.prototype.toString.call(/test/i); // [object RegExp]
Object.prototype.toString.call(true); // [object Boolean]
Object.prototype.toString.call(null); // [object Null]
Object.prototype.toString.call(); // [object Undefined]
var getType = function (elem) {
return Object.prototype.toString.call(elem).slice(8, -1);
};
var isObject = function (elem) {
return getType(elem) === 'Object';
};
if (isObject(person)) {
person.getName();
}
Dates
Formatting Dates - Vanilla JS
MDM: Intl.DateTimeFormat()
https://gomakethings.com/how-to-create-a-clock-with-vanilla-js/
IIFE - Immediately Invoked Function Expression
https://developer.mozilla.org/en-US/docs/Glossary/IIFE
https://vanillajstoolkit.com/boilerplates/iife/
(function () {
'use strict';
// Code goes here...
})();