Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
shaunwallace / gist:eed7b0834ff3457b0483
Created November 19, 2014 14:13
Array.prototype.forEach()
function double(element, index, array) {
console.log('elem['+ index + '] ', element * 2);
}
[1,2,3,4,5].foEach(double); // returns elem[0] 2 elem[1] 4 elem[2] 6 elem[3] 8 elem[4] 10
@shaunwallace
shaunwallace / gist:f79dde0fd548925af6f0
Created November 19, 2014 14:04
Array.prototype.filter()
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].filter(isBigEnough); // returns [12,130,44]
@shaunwallace
shaunwallace / gist:b995544c6b6934c5851e
Created November 19, 2014 14:03
Array.prototype.every()
function isBigEnough(element, index, array) {
return element >= 10;
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough); // passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough); // passed is true
// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
@shaunwallace
shaunwallace / gist:81e9d55556e72f95a3d1
Created November 19, 2014 13:58
String.prototype.trim()
var trimmed = ' foo ';
trimmed = trimmed.trim(); // returns 'foo'
// ES5 also allows for access to characters at specific indices via the bracket notation
trimmed[0]; // returns "f"
@shaunwallace
shaunwallace / gist:04f221d06c7934cb7ba9
Created November 19, 2014 13:54
Function.prototype.bind()
function Shape() {
this.width = 100;
this.height = 100;
}
Shape.prototype.area = function() {
return this.width * this.height;
}
var square = new Shape();
@shaunwallace
shaunwallace / gist:095d8d385503ef7214df
Created November 19, 2014 13:51
Object.isFrozen()
var obj13 = Object.create({});
obj13.foo = 'x';
obj13.bar = 'y';
Object.freeze( obj13 ); // freezes the object completely
Object.isFrozen( obj13 ); // returns true
Object.isFrozen({}); // returns false because a newly created object is extensible by default
// create empty object
var obj13 = Object.create({});
// adding some properties
obj13.foo = 'x';
obj13.bar = 'y';
Object.freeze( obj13 ); // freezes the object completely
// after being frozen we can not alter the object
obj13.baz = 'z';
obj13.foo = false;
var obj12 = { foo : 200 };
obj12.foo = false;
obj12; // returns false
Object.seal( obj12 );
obj12.foo = 100;
obj12.foo; // returns 100
delete obj12.foo; // returns false
@shaunwallace
shaunwallace / gist:3d1b475128f9b842df78
Created November 19, 2014 12:48
Object.preventExtensions()
var obj11 = Object.create(Object.prototype, { foo : {
value : 27
},
bar : {
value : 39,
enumerable : true
}
});
Object.preventExtensions( obj11 );