This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isBigEnough(element, index, array) { | |
return element >= 10; | |
} | |
[12, 5, 8, 130, 44].filter(isBigEnough); // returns [12,130,44] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Shape() { | |
this.width = 100; | |
this.height = 100; | |
} | |
Shape.prototype.area = function() { | |
return this.width * this.height; | |
} | |
var square = new Shape(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var obj11 = Object.create(Object.prototype, { foo : { | |
value : 27 | |
}, | |
bar : { | |
value : 39, | |
enumerable : true | |
} | |
}); | |
Object.preventExtensions( obj11 ); |