Last active
October 20, 2016 15:48
-
-
Save branneman/6158610 to your computer and use it in GitHub Desktop.
ECMAScript 5 In code examples
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
/** | |
* Strict mode | |
* Opt in to a restricted variant of JavaScript. | |
*/ | |
'use strict'; | |
(function() { 'use strict'; }); | |
/** | |
* Array.prototype.forEach() | |
* Executes a provided function once per array element. | |
* thisArg: the value to use as this when executing callback. | |
*/ | |
['aap', 'noot', 'mies'].forEach(function(value, index, arr) { | |
console.log('a[' + index + '] = ' + value); | |
}, thisArg); | |
/** | |
* Array.prototype.map() | |
* Creates a new array by calling a function for every item. | |
* thisArg: the value to use as this when executing callback. | |
*/ | |
['3.14', '2.71'].map(function(value, index, arr) { | |
return parseFloat(value); | |
}, thisArg); | |
/** | |
* Array.prototype.filter() | |
* Filters items of an array. | |
* thisArg: the value to use as this when executing callback. | |
*/ | |
[0, 1, 2, 3, 4].filter(function(value, index, arr) { | |
return Math.round(Math.random()); | |
}, thisArg); | |
/** | |
* Array.prototype.reduce() | |
* Array.prototype.reduceRight() | |
* Iteratively reduce an array to a single value. | |
* Reduce works ltr, reduceRight works rtl. | |
*/ | |
[0, 1, 2, 3, 4].reduce(function(prev, next, index, arr) { | |
return prev + next; | |
}); | |
[0, 1, 2, 3, 4].reduceRight(function(prev, next, index, arr) { | |
return prev + next; | |
}); | |
/** | |
* Array.prototype.every() | |
* Array.prototype.some() | |
* Test whether every/some items passes a test. | |
* thisArg: the value to use as this when executing callback. | |
*/ | |
var fn = function(value, index, arr) { | |
return !!value; | |
}; | |
[1, 2, 0, 4, 5].every(fn, thisArg); | |
[1, 2, 3, 0, 5].some(fn, thisArg); | |
/** | |
* Array.prototype.indexOf() | |
* Array.prototype.lastIndexOf() | |
* Find the index of the item that equals a value. | |
*/ | |
[0, 2, 2, 4, 6].indexOf(6); | |
[0, 1, 1, 3, 4].lastIndexOf(1); | |
/** | |
* Array.isArray() | |
* Checks if a variable is a real array. | |
*/ | |
Array.isArray({length: 2, 0: 'aap', 1: 'noot'}); | |
/** | |
* Function.prototype.bind() | |
* Proxy a function with given context and arguments. | |
*/ | |
(function(arg) { | |
return 'aap ' + this.value + ' mies ' + arg; | |
}.bind({value: 'noot'}))('wim'); | |
/** | |
* Object.preventExtensions() | |
* Object.isExtensible() | |
* Prevents future property additions from occurring, | |
* cannot be undone. | |
*/ | |
var zus = {jet: 'teun'}; | |
Object.preventExtensions(zus); | |
Object.isExtensible(zus); | |
zus.vuur = 'gijs'; // no-op | |
/** | |
* Object.seal() | |
* Object.isSealed() | |
* Prevents any changes to properties or descriptors of the object, | |
* values can still be changed. | |
*/ | |
var mies = Object.seal({wim: 'zus'}); | |
Object.isSealed(mies); | |
/** | |
* Object.freeze() | |
* Object.isFrozen() | |
* Prevents any changes to the object. | |
*/ | |
var wim = Object.freeze({zus: 'jet'}); | |
Object.isFrozen(wim); | |
/** | |
* JSON | |
* parse() — Parse a string as JSON. | |
* stringify() — Convert a value to JSON. | |
*/ | |
JSON.parse('[1, 2, 3, 4]'); | |
JSON.stringify({aap: 'monkey'}); | |
/** | |
* Date object | |
* now() — Get current timestamp | |
* parse() — Now accepts subset ISO 8601 formatted dates | |
* toJSON() — Get JSON representation | |
* toISOString() — Get ISO 8601 formatted date | |
*/ | |
Date.now(); | |
Date.parse('2013-08-05'); | |
(new Date()).toJSON(); | |
(new Date()).toISOString(); | |
/** | |
* Property access on strings | |
*/ | |
var str = 'aap noot mies wim zus jet teun vuur gijs lam'; | |
str[9] + str[1] + str[2]; | |
/** | |
* String.prototype.trim() | |
* Trim whitespace of the beginning and end of a string. | |
*/ | |
' aap noot mies '.trim(); | |
/** | |
* Zero-width chars in identifiers | |
*/ | |
var $\u200c = 'a', | |
teun = {_\u200d: 'vuur'}; | |
/** | |
* Reserved words as property names | |
*/ | |
var jet = {if: 3, throw: .14}; | |
/** | |
* Multiline string literals | |
*/ | |
var leesplankje = 'aap \ | |
noot mies'; | |
/** | |
* `with` keyword is now deprecated | |
*/ | |
with ({aap: 'noot'}) { | |
console.log(aap); | |
} | |
/** | |
* Global objects are now read only | |
* Both statements are a no-op. | |
*/ | |
Infinity = 42; | |
var NaN = 'aap', undefined = 'noot'; | |
/** | |
* Removed object properties | |
* Both statements return undefined. | |
*/ | |
({}).__parent__; | |
({}).__count__; |
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
/** | |
* Resources: | |
* http://kangax.github.io/es5-compat-table/ | |
* https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js | |
* http://ejohn.org/blog/ecmascript-5-objects-and-properties/ | |
* http://www.jayway.com/2011/04/05/what-is-new-in-ecmascript-5/ | |
*/ | |
/** | |
* Object.create() | |
* Create a new object, specifying prototype and properties. | |
* @todo example | |
*/ | |
Object.create(); | |
Object.clone(); | |
Object.defineProperty(); | |
Object.defineProperties(); | |
Object.getOwnPropertyDescriptor(); | |
Object.getPrototypeOf(); | |
Object.keys(); | |
Object.getOwnPropertyNames(); | |
// Getters & Setters as property initializer | |
// Getters & Setters as named properties | |
Number.prototype.toFixed(); | |
toObject(); | |
toInteger(); | |
toPrimitive(); | |
isPrimitive(); | |
/** | |
* Property descriptor | |
* writable — Can the value be changed? | |
* enumerable — Will it appear in for-in and Object.keys() | |
* configurable — Can the property be removed? | |
* get/set — | |
* @todo finish | |
*/ | |
var myÜberDescriptor = { | |
writable: false, | |
enumerable: true, | |
configurable: false, | |
get: function(v) { | |
return this.noot; | |
}, | |
set: function(v) { | |
this.noot = v; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment