Skip to content

Instantly share code, notes, and snippets.

@IamAdamJowett
Last active August 29, 2015 14:10
Show Gist options
  • Save IamAdamJowett/6d9b3f82a96cefa58d1a to your computer and use it in GitHub Desktop.
Save IamAdamJowett/6d9b3f82a96cefa58d1a to your computer and use it in GitHub Desktop.
JavaScript tips

Safe function calls

As described by David Walsh: http://davidwalsh.name/function-attempt

function attempt(fn, args, binding) {
	try {
		return fn.apply(binding, args);
	} catch(e) {
		console.log('Exception, fix me please', e);
	}
}

// Use it!
attempt(function() {
	/* volatile stuff */
}, ['argOne', someVar], this);

Rotate an array

To rotate (loop) an array with JavaScript:

var pages = ['page1', 'page2', 'page3'];

function rotate(arr) {
    return arr.push(arr.shift());
}

rotate(pages); // ['page2', 'page3', 'page1']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment