Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combined http://learnjs.org/ examples
// Getting an Object’s enumerable properties (ES5)
var obj = {};
Object.defineProperty(obj, "foo", { value: 2, enumerable: true });
Object.defineProperty(obj, "bar", { value: 3, enumerable: true });
Object.defineProperty(obj, "baz", { value: 4, enumerable: false });
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
// Type less using “with”
with(Math) {
console.log(round(3.5));
console.log(floor(3.8));
console.log(ceil(3.2));
}
// Learning how to use ternary
var userAgent = "iPhone";
var redirect = (userAgent == "iPhone" || userAgent == "Android") ? "phone.html" : "desktop.html";
console.log(redirect);
// Called as function or constructor?
function woot() {
if(this instanceof woot) {
console.log("called as constructor");
} else {
console.log("called as function");
}
}
woot();
// Iterating over function arguments
function test() {
window.args = arguments;
}
test(1, 2, 3);
var args = Array.prototype.slice.call(args);
args.forEach(function(arg) { console.log(arg); });
// Passing arguments to another function
function passedTo() {
console.log("called with following arguments:", arguments);
}
function pass() {
passedTo.apply(null, arguments);
}
pass(1, 2, 3);
// The speed of (function() {})
var startTime = +new Date;
var tmp = [];
for(var i=0; i<1234567; i++) { tmp.push(i); }
console.log("it took:", +new Date - startTime, "ms");
var startTime = +new Date;
var tmp = [];
(function() {
for(var i=0; i<1234567; i++) { tmp.push(i); }
})();
console.log("it took:", +new Date - startTime, "ms");
// Find and remove an element from an array
var arr = ["one", "two", "three", "four", "five"];
arr.splice(arr.indexOf("four"), 1);
console.log(arr);
// String multiplication with JS
new Array(100).join("♥ Unicorns ♥");
// Array as stack
var stack = [];
stack.push("foo");
stack.push("bar");
stack.push("baz");
stack.pop();
stack.push("test");
console.log(stack);
// Appending arrays
var a = [1,2,3,4];
var b = [4,5,6,7];
Array.prototype.push.apply(a, b);
uneval(a);
// Filter falsy values from an array
var arr = ["foo", "", 1, 0, NaN, true, false, null, undefined, {foo:"bar"}];
console.log(arr.filter(Boolean));
// Inheriting without calling the parent constructor
// A stupid instanceof trick
function Foo() {
this.foo = true;
}
var a = new Foo();
console.log(a instanceof Foo);
var Bar = Foo;
console.log(a instanceof Bar);
// What does setInterval do?
var n = 1;
var fn = function() {
if (n == 5) { clearTimeout(i); }
console.log("iteration", n++);
};
var i = setInterval(fn, 500);
// What does setTimeout do?
var fn = function() { console.log("hello world"); };
var timer = setTimeout(fn, 1000);
clearTimeout(timer);
// How do I use default arguments
var foo = function(a) {
a = a || 10;
return a * 10;
};
console.log(foo());
console.log(foo(5));
var x = 0 || 0 || false || true || 10;
console.log(x);
var y = 1 && 2 && 3 && 3 && 4 && 5 && false;
console.log(y);
// Generate unique random alphanumeric strings in JavaScript
function passgen(length) {
var s = "";
var randomchar = function() {
var n = Math.floor(Math.random() * 62);
return (n < 10 ? n : ((n < 36) ? String.fromCharCode(n+55) : String.fromCharCode(n+61)));
};
while(s.length < length) {
s += randomchar();
}
return s;
}
console.log(passgen(8));
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.