Last active
October 11, 2015 22:28
-
-
Save zhuochun/3929640 to your computer and use it in GitHub Desktop.
JavaScript Snippets from Everywhere
This file contains 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
+1 === 1; | |
+0 === 0; | |
+undefined == NaN; | |
+null === 0; | |
+({1:2,2:3}).length == NaN | |
// Underscore -> https://github.com/documentcloud/underscore/blob/master/underscore.js#L80 | |
var each = _.each = _.forEach = function(obj, iterator, context) { | |
if (obj == null) return; | |
if (nativeForEach && obj.forEach === nativeForEach) { | |
obj.forEach(iterator, context); | |
} else if (obj.length === +obj.length) { | |
for (var i = 0, l = obj.length; i < l; i++) { | |
if (iterator.call(context, obj[i], i, obj) === breaker) return; | |
} | |
} else { | |
for (var key in obj) { | |
if (_.has(obj, key)) { | |
if (iterator.call(context, obj[key], key, obj) === breaker) return; | |
} | |
} | |
} | |
}; |
This file contains 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
// Instead of | |
Boolean(obj); | |
// Can use | |
!!obj; |
This file contains 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
!!~-1 === false; // only -1 will return false | |
/* | |
Explanation -> http://stackoverflow.com/a/10582345/889972 | |
-1 = 1111 1111 1111 1111 1111 1111 1111 1111b | |
~-1 = 0000 0000 0000 0000 0000 0000 0000 0000b // ~ = bitwise not = invert all bits | |
*/ | |
!!~2 === true; | |
!!~NaN === true; | |
!!~undefined === true; |
This file contains 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
// http://www.cnblogs.com/linjisong/archive/2012/08/25/2654852.html | |
// + : 除了加(+)之外,如果操作数不是Number类型, | |
// 会自动调用Number()转换为Number类型再进行计算。 | |
// -: 对于加减(+-),除了作为算术运算符。还可以作为一元操作符( | |
// 一元减则是在一元加的基础之上再取其相反数。)。 | |
// 当然,由于字符串操作中对加号(+)的重载,还可以用于将任意数值 | |
// (的字符串)相连,这也是第1点中为什么要除了加(+),它在含有非 | |
// Number类型值时,会将所有操作数转换为字符串相连接。 | |
+1 === 1; //true | |
+0 === 0; //true | |
+undefined == NaN; //true | |
+null === 0; //true | |
+({1:2,2:3}).length === NaN; //true | |
+"0xff" === 255; //true | |
+"0x10" === 16; //true | |
+"010" === 10; //true | |
+"100" === 100; //true | |
// Usage in Underscore -> | |
// https://github.com/documentcloud/underscore/blob/master/underscore.js#L80 | |
var each = _.each = _.forEach = function(obj, iterator, context) { | |
if (obj == null) return; | |
if (nativeForEach && obj.forEach === nativeForEach) { | |
obj.forEach(iterator, context); | |
} else if (obj.length === +obj.length) { | |
for (var i = 0, l = obj.length; i < l; i++) { | |
if (iterator.call(context, obj[i], i, obj) === breaker) return; | |
} | |
} else { | |
for (var key in obj) { | |
if (_.has(obj, key)) { | |
if (iterator.call(context, obj[key], key, obj) === breaker) return; | |
} | |
} | |
} | |
}; |
This file contains 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
// Sugar -> https://github.com/zhuochun/Sugar/blob/master/release/1.3/sugar-1.3-full.development.js#L29 | |
var ClassNames = 'Array,Boolean,Date,Function,Number,String,RegExp'.split(','); | |
var isArray = buildClassCheck(ClassNames[0]); | |
var isBoolean = buildClassCheck(ClassNames[1]); | |
var isDate = buildClassCheck(ClassNames[2]); | |
var isFunction = buildClassCheck(ClassNames[3]); | |
var isNumber = buildClassCheck(ClassNames[4]); | |
var isString = buildClassCheck(ClassNames[5]); | |
var isRegExp = buildClassCheck(ClassNames[6]); | |
function buildClassCheck(type) { | |
return function(obj) { | |
return isClass(obj, type); | |
} | |
} | |
function isClass(obj, str) { | |
return Object.prototype.toString.call(obj) === '[object '+str+']'; | |
} | |
// Underscore | |
_.isNaN = function(obj) { | |
return _.isNumber(obj) && obj != +obj; | |
}; | |
_.isBoolean = function(obj) { | |
return obj === true || obj === false || toString.call(obj) == '[object Boolean]'; | |
}; | |
_.isUndefined = function(obj) { | |
return obj === void 0; | |
}; | |
_.isElement = function(obj) { // DOM Element | |
return !!(obj && obj.nodeType === 1); | |
}; | |
_.isObject = function(obj) { | |
return obj === Object(obj); | |
}; | |
// jQuery | |
isEmptyObject = function( obj ) { | |
var name; | |
for ( name in obj ) { | |
return false; | |
} | |
return true; | |
} |
This file contains 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
// Instead of using | |
Math.floor(3.14159); // = 3 | |
// Can also use | |
0 | 3.14159; // = 3 | |
// Or | |
~~3.14156; // = 3 | |
/* | |
Explanation: http://stackoverflow.com/questions/5533387/javascript-operator | |
When using the bitwise or operator in JavaScript, the runtime has to convert | |
the operands to 32 bit integers before it can proceed. Doing this chops away | |
the fractional part, leaving you with just an integer left behind. Bitwise OR | |
of zero is a no op (well, a no op in a language that has true integers) but | |
has the side effect of flooring in JavaScript. | |
*/ | |
// Underscore -> https://github.com/documentcloud/underscore/blob/master/underscore.js#L1004 | |
_.random = function(min, max) { | |
if (max == null) { | |
max = min; | |
min = 0; | |
} | |
return min + (0 | Math.random() * (max - min + 1)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment