Created
June 22, 2014 09:49
-
-
Save toctan/4dbcf32e1703446bcd2b to your computer and use it in GitHub Desktop.
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
// Numbers | |
// Only one number type, 64-bit floating point, no integers | |
// first class object | |
// .01024e4 | |
// 2.024e+3 | |
// 10.24E2 | |
// 1024E+1 | |
// 1024.e0 | |
// 1024.e0 | |
// 1024.00 | |
// 1024 | |
// 20140e-1 | |
// Associative Law does not hold | |
var x = 0.1, | |
y = 0.2, | |
z = 0.3; | |
(x + y) + z === x + (y + z); // false | |
// Number methods | |
x.toExponential(); | |
x.toFixed(); | |
x.toLocaleString(); | |
x.toPrecision(); | |
x.toString(); | |
x.valueOf(); | |
// argument Number prototype | |
if (!Number.prototype.trunc) { | |
Number.prototype.trunc = function (number) { | |
return Math[number >= 0 ? 'floor' : 'ceiling'](number); | |
}; | |
} | |
// NaN | |
// Not a Number, result of undefined or erroneous operations | |
// Toxic, any arithmetic with NaN will have NaN as a result | |
// Not equal to anything, including itself | |
NaN === NaN; // false | |
NaN !== NaN; // true | |
// Math object | |
// Legacy of Java, this should be in the Number.prototype | |
// methods | |
Math.abs(); | |
Math.acos(); | |
Math.asin(); | |
Math.atan(); | |
Math.atan2(); | |
Math.ceil(); | |
Math.cos(); | |
Math.exp(); | |
Math.floor(); | |
Math.log(); | |
Math.max(); | |
Math.min(); | |
Math.pow(); | |
Math.random(); | |
Math.round(); | |
Math.sin(); | |
Math.sqrt(); | |
Math.tan(); | |
// Constants | |
Math.E; | |
Math.LN10; | |
Math.LN2; | |
Math.LOG2E; | |
Math.LOG10E; | |
Math.PI; | |
Math.SQRT1_2; | |
Math.SQRT2; | |
// Boolean | |
true; | |
false; | |
// Falsy values, all other values are truthy | |
false; | |
null; | |
undefined; | |
''; | |
0; | |
NaN; | |
// String | |
// A sequence of 0 or more characters enclosed with single or double | |
// quote with \ escapement, no awareness of surrogate pairs | |
// No separate character type | |
// Immutable, similar strings are equal( === ) | |
// Convert a number to a string | |
var str = num.toString(); | |
var str = String(num); | |
// Convert a string to a number | |
var num = Number(str); | |
var num = +str; | |
// Be aware of the parseInt Radix | |
parseInt(str, 10); | |
parseInt("12em"); // 12 | |
parseInt("08"); // 0 | |
parseInt("08", 10); // 8 | |
// length property | |
str.length; | |
// methods | |
str.charAt(); | |
str.charCodeAt(); | |
str.concat(); | |
str.indexOf(); | |
str.lastIndexOf(); | |
str.localeCompare(); | |
str.match(); | |
str.replace(); | |
str.search(); | |
str.slice(); | |
str.split(); | |
str.substr(); | |
str.substring(); | |
str.toLocaleLowerCase(); | |
str.toLocaleUpperCase(); | |
str.toLowerCase(); | |
str.toString(); | |
str.toUpperCase(); | |
str.trim(); | |
str.trimLeft(); | |
str.trimRight(); | |
str.valueOf(); | |
// Arrays | |
// Array inherits from Object, indexes are converted to strings and | |
// used as names for retrieving values | |
// Very efficient for sparse arrays, not so efficient in most other cases. | |
var arr = [1, 2, 3]; | |
// append new items: | |
arr[arr.length] = 4; | |
// Do not use for in with arrays, for the order is not guaranteed | |
for (i = 0; i < arr.length; i += 1) { | |
// ... | |
} | |
// methods | |
arr.concat(); | |
arr.every(); | |
arr.filter(); | |
arr.forEach(); | |
arr.indexOf(); | |
arr.join(); | |
arr.lastIndexOf(); | |
arr.map(); | |
arr.pop(); | |
arr.push(); | |
arr.reduce(); | |
arr.reduceRight(); | |
arr.reverse(); | |
arr.shift(); | |
arr.slice(); | |
arr.some(); | |
arr.sort(); | |
arr.splice(); | |
arr.toLocaleString(); | |
arr.toString(); | |
arr.unshift(); | |
// New in ES5 | |
Array.isArray([]); // true | |
// notes on sort() | |
[4, 8, 15, 16, 23, 42].sort(); // [15, 16, 23, 4, 42, 8] | |
[4, 8, 15, 16, 23, 42].sort(function(x, y) { return x > y; }); | |
// The right way to remove an element | |
var arr = ['a', 'b', 'c', 'd']; | |
delete arr[1]; // ['a', undefined, 'c', 'd'] | |
arr.splice(1, 1); // ['a', 'c', 'd'] | |
// Date | |
// Based on Java's Date class | |
date.now(); | |
date.parse(); | |
date.getDate(); | |
date.getDay(); | |
date.getFullYear(); | |
date.getHours(); | |
date.getMilliseconds(); | |
date.getMinutes(); | |
date.getMonth(); | |
date.getSeconds(); | |
date.getTime(); | |
date.getTimezoneOffset(); | |
date.getUTCDate(); | |
date.getUTCDay(); | |
date.getUTCFullYear(); | |
date.getUTCHours(); | |
date.getUTCMilliseconds(); | |
date.getUTCMinutes(); | |
date.getUTCMonth(); | |
date.getUTCSeconds(); | |
date.getYear(); | |
date.setDate(); | |
date.setFullYear(); | |
date.setHours(); | |
date.setMilliseconds(); | |
date.setMinutes(); | |
date.setMonth(); | |
date.setSeconds(); | |
date.setTime(); | |
date.setUTCDate(); | |
date.setUTCFullYear(); | |
date.setUTCHours(); | |
date.setUTCMilliseconds(); | |
date.setUTCMinutes(); | |
date.setUTCMonth(); | |
date.setUTCSeconds(); | |
date.setYear(); | |
date.toDateString(); | |
date.toGMTString(); | |
date.toISOString(); | |
date.toJSO(); | |
date.toLocaleDateString(); | |
date.toLocaleString(); | |
date.toLocaleTimeString(); | |
date.toString(); | |
date.toTimeString(); | |
date.toUTCString(); | |
date.valueOf(); | |
// RegExp | |
// All values are objects, except null and undefined | |
null; // a value that isn't anything | |
undefined; // default values for variables and | |
// parameters, also the value of | |
// missing members in objects | |
typeof object; // 'object' | |
typeof func; // 'function' | |
typeof array; // 'object' | |
typeof number; // 'number' | |
typeof string; // 'string' | |
typeof boolean; // 'boolean' | |
typeof null; // 'object' | |
typeof undefined; // 'undefined' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment