This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fetchLastScore() { | |
| return () => { | |
| console.log(arguments[arguments.length - 1]); | |
| } | |
| } | |
| fetchLastScore(42, 68, 49, 83, 72)(); // 72 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // FIRST FORM: | |
| // Wrap the function expression in parentheses | |
| // The invocation expression comes afterwards | |
| (function(a, b) { | |
| // ...function body here | |
| })(arg1, arg2); | |
| // SECOND FORM: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const flattenDeep = (...args) => args.reduce( | |
| (a, b) => [].concat(a, Array.isArray(b) ? flattenDeep(...b) : b) | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const country = { | |
| name: 'Nigeria', | |
| region: 'Africa', | |
| codes: { | |
| cca2: 'NG', | |
| dialcode: '+234' | |
| }, | |
| cities: [ | |
| 'Lagos', | |
| 'Abuja', |