Skip to content

Instantly share code, notes, and snippets.

@danielmascena
Created February 6, 2018 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmascena/8b98b81dd5e179c513c9d89197e844d6 to your computer and use it in GitHub Desktop.
Save danielmascena/8b98b81dd5e179c513c9d89197e844d6 to your computer and use it in GitHub Desktop.
Chunk Of Code.info
/**
* An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript programming language idiom
* which produces a lexical scope using JavaScript's function scoping.
*/
// keep things outside the global scope plz
(function (window, document, undefined) {
'use strict';
/**
* Selectors (caching your selectors into variables)
*/
var menu = document.querySelector('.menu');
var users = document.querySelectorAll('.user');
var signout = document.querySelector('.signout');
/**
* Methods
*/
function toggleMenu (event) {
if (!this.classList.contains('active')) {
this.classList.add('active');
}
event.preventDefault();
}
function showUsers (users) {
for (var i = 0; i < users.length; i++) {
var self = users[i];
self.classList.add('visible');
}
}
function signout (users) {
var xhr = new XMLHttpRequest();
// TODO: finish signout
}
/**
* Events/APIs/init
*/
menu.addEventListener('click', toggleMenu, false);
signout.addEventListener('click', signout, false);
showUsers(users);
})(window, document);
// (() => { /* ... */ })(); // With ES6 arrow functions (though parentheses only allowed on outside)
//You can grab the .bind() polyfill here so that all browsers can use .bind() (as it’s current IE9+ and all modern browsers).
//You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of bind() in implementations that do not natively support it.
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();
return fBound;
};
}
// OR
element.addEventListener('click', function () {
toggleMenu(param1, param2);
}, false);
@danielmascena
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment