Skip to content

Instantly share code, notes, and snippets.

View pseudosavant's full-sized avatar

Paul Ellis pseudosavant

View GitHub Profile
@pseudosavant
pseudosavant / globalObject.js
Last active January 2, 2016 20:59
Get access to the global object in whatever enviroment you are using. i.e. In a browser it will point to `window`, in Node.js will point to `global`.
var global = (function(g) {
return g;
})(this);
global.a = 'Hello World';
console.log(a); // 'Hello World'
@pseudosavant
pseudosavant / declareGlobalVariables.js
Last active October 1, 2023 21:51
Explicitly declare global variables inside an immediately invoked function expression (IIFE).Benefits:1. It is very obvious when you are intending to export a variable from a function.2. You can create global variables and your static code analyzer (JSLint/JSHint) won't complain.
(function(global){
var x = 2,
y = Math.pow(x, 2);
global.z = y;
})(this);
console.log(x); // undefined
console.log(y); // undefined
console.log(z); // 4
@pseudosavant
pseudosavant / exportFunctionsToGlobal.js
Last active January 2, 2016 20:59
Selectively export functions to the global scope
(function(global){
var who = 'World';
var rename = function(s) {
who = s;
};
var welcome = function(){
return 'Hello ' + who;
};
@pseudosavant
pseudosavant / defaultVariables.js
Last active January 2, 2016 20:59
Set default variables in JavaScript
var welcome = function(name) {
name = name || 'World';
return 'Hello ' + name;
};
console.log(welcome()); // 'Hello World'
console.log(welcome('foo')); // 'Hello foo'
@pseudosavant
pseudosavant / cacheElements.js
Last active January 2, 2016 20:59
Cache jQuery objects or DOM elements you use in multiple places in your code.
// jQuery
var $myFormButton = $('.myForm input[type=button]'); // Potentially slow query
$myFormButton.val('Submit');
// lots of code later
$myFormButton.val('Processing'); // Didn't need to lookup the element(s) again
$myFormButton.attr('disabled', true); // Didn't need to lookup the element(s) again
// DOM
@pseudosavant
pseudosavant / findChildren.js
Last active January 2, 2016 20:59
Find elements without searching the entire DOM
// jQuery
var $myForm = $('#myForm');
// Find child elements in the form without searching the entire DOM
$myForm.children('input[type=text]').focus();
$myForm.children('input[type=button]').val('Submit');
// DOM
var $myForm = document.querySelector('#myForm');
@pseudosavant
pseudosavant / covertToArray.js
Last active January 2, 2016 21:19
Convert array-like objects to an array.
// Convert a set of DOM elements to an array
var domElements = document.querySelectorAll('li');
console.log(domElements.pop()); // Undefined
var arrayOfDomElements = Array.prototype.slice.apply(domElements);
console.log(arrayOfDomElements.pop()); // [object HTMLLIElement]
// Convert function arguments to an array
var myFunction = function() {
@pseudosavant
pseudosavant / forInLoops.js
Last active January 2, 2016 21:29
The best/safest way to do `for x in y` loops.
var obj = {
welcome: 'Hello',
name: 'World'
},
value;
for (var prop in obj) {
// Check if the object has that property so that prototypes aren't included.
if(obj.hasOwnProperty(prop)) {
value = obj[prop];
@pseudosavant
pseudosavant / arrayLoop.js
Created January 10, 2014 22:35
Fastest way to loop over an array or array-like object
var myArray = [
'Hello',
'World',
'foo',
'bar'
];
// Only check the `length` property once by setting `l` at the same time as `i`.
for (var i = 0, l = myArray.length; i < l; i++) {
console.log(myArray[i]);
@pseudosavant
pseudosavant / breakLoops.js
Created January 10, 2014 22:44
Break out of a loop early once a result has been found.
var myArray = [
'Hello',
'World',
'foo',
'bar'
];
// Only check the `length` property once by setting `l` at the same time as `i`.
for (var i = 0, l = myArray.length; i < l; i++) {
console.log(i);