Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Created August 10, 2013 08:09
Show Gist options
  • Save sudodoki/6199556 to your computer and use it in GitHub Desktop.
Save sudodoki/6199556 to your computer and use it in GitHub Desktop.
Useful snippets I'll lose other way I've seen in David Walsh's post http://tech.pro/tutorial/1453/7-javascript-basics-many-developers-aren-t-using-properly
// Cloning Array
var clone = myArray.slice(0); // naive clone
// Merging 2 arrays
var mergeTo = [4,5,6],
var mergeFrom = [7,8,9];
Array.prototype.push.apply(mergeTo, mergeFrom);
# Array::push.apply mergeTo, mergeFrom
mergeTo; // is: [4, 5, 6, 7, 8, 9]
// Check for truthy key in object
if("geolocation" in navigator) {
// Do some stuff
}
// 'if (navigator.geolocation)' case - while that works correctly, it isn't always efficient, as that method of object detection
// can initialize resources in the browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment