Skip to content

Instantly share code, notes, and snippets.

View gladchinda's full-sized avatar

Glad Chinda gladchinda

View GitHub Profile
function fetchLastScore() {
return () => {
console.log(arguments[arguments.length - 1]);
}
}
fetchLastScore(42, 68, 49, 83, 72)(); // 72
// FIRST FORM:
// Wrap the function expression in parentheses
// The invocation expression comes afterwards
(function(a, b) {
// ...function body here
})(arg1, arg2);
// SECOND FORM:
// IIFE: With Arrow Function
// The arrow function is called immediately with a list of arguments
// and the return value is assigned to the `compute` variable
const compute = ((...numbers) => {
// Private members
const length = numbers.length;
const min = Math.min(...numbers);
// WITHOUT ARROW FUNCTION
const sum = numbers.reduce(function(a, b) {
return a + Number(b);
}, 0);
// WITH ARROW FUNCTION
const sum = numbers.reduce((a, b) => a + Number(b), 0);
const flattenDeep = (...args) => args.reduce(
(a, b) => [].concat(a, Array.isArray(b) ? flattenDeep(...b) : b)
);
function Person(name) {
this.name = name;
}
var person = Person('Glad Chinda');
console.log(person); // undefined
console.log(name); // "Glad Chinda"
console.log(window.name); // "Glad Chinda"
function ScrollController(offset) {
this.offsets = { offsetY: offset };
}
ScrollController.prototype.registerScrollHandler = function() {
window.addEventListener('scroll', function(event) {
if (window.scrollY === this.offsets.offsetY) {
console.log(`${this.offsets.offsetY}px`);
}
}, false);
// Using .bind() on event listener to resolve the value of `this`
ScrollController.prototype.registerScrollHandler = function() {
this.element.addEventListener('scroll', (function(event) {
if (window.scrollY === this.offsets.offsetY) {
console.log(`${this.offsets.offsetY}px`);
}
}).bind(this), false);
}
// Using arrow function for event listener
ScrollController.prototype.registerScrollHandler = function() {
this.element.addEventListener('scroll', event => {
if (window.scrollY === this.offsets.offsetY) {
console.log(`${this.offsets.offsetY}px`);
}
}, false);
}
const country = {
name: 'Nigeria',
region: 'Africa',
codes: {
cca2: 'NG',
dialcode: '+234'
},
cities: [
'Lagos',
'Abuja',