Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.