Skip to content

Instantly share code, notes, and snippets.

@ilovejs
Last active December 20, 2015 22:49
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 ilovejs/6208345 to your computer and use it in GitHub Desktop.
Save ilovejs/6208345 to your computer and use it in GitHub Desktop.
str.replace(/David/g, "Darren"); // "Darren is an Arsenal fan, which means Darren is great"
var clone = myArray.slice(0); // naive clone
var nodesArr = Array.prototype.slice.call(document.querySelectorAll("div")); // "true" array of DIVs
var argsArr = Array.prototype.slice.call(arguments); // changes arguments to "true" array
[
{ name: "Robin Van PurseStrings", age: 30 },
{ name: "Theo Walcott", age: 24 },
{ name: "Bacary Sagna", age: 28 }
].sort(function(obj1, obj2) {
// Ascending: first age less than the previous
return obj1.age - obj2.age;
});
// Returns:
// [
// { name: "Theo Walcott", age: 24 },
// { name: "Bacary Sagna", age: 28 },
// { name: "Robin Van PurseStrings", age: 30 }
// ]
var mergeTo = [4,5,6],
var mergeFrom = [7,8,9];
Array.prototype.push.apply(mergeTo, mergeFrom);
mergeTo; // is: [4, 5, 6, 7, 8, 9]
// Efficient Feature/Object Property Detection
if("geolocation" in navigator) {
// Do some stuff
}
$("a.trigger").on("click", function(e) {
//e.stop();
preventDefault
});
var nodesArr = Array.prototype.slice.call(document.querySelectorAll("div")); // "true" array of DIVs
var argsArr = Array.prototype.slice.call(arguments); // changes arguments to "true" array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment