Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active December 15, 2015 02:29
Show Gist options
  • Select an option

  • Save monjer/5187739 to your computer and use it in GitHub Desktop.

Select an option

Save monjer/5187739 to your computer and use it in GitHub Desktop.
JS常用快捷方法
(function(opt_ns , opt_wind){
var g_win = opt_wind || top ;
var g_doc = g_win.document ;
var toString = Object.prototype.toString ;
var types = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
var classToType = {};// javascript 类型名称的精简版映射
for(var i = 0 ; i < types.length ; i++){
classToType["[object "+types[i]+"]"] = types[i].toLowerCase() ;
}
var lang = {
/**
* 返回对象的类型描述字符
* @param obj
* @returns{'boolean'||'number'||'string'||'function'||'array'||'date'||'regexp'||'object'||'error'||'null'||'undefined'}
* @reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
*/
type:function(obj){
if ( obj == null ) {
return String( obj );
}
return typeof obj === "object" || typeof obj === "function" ?
classToType[ toString.call(obj) ] || "object" :
typeof obj;
},
/**
* 是否为对象类型
* @return {Boolean}
* @param {Object} obj
*/
isObject:function(obj){
return toString.call(obj) == "[object Object]";
},
/**
* 是否为数值类型
* @return {Boolean}
* @param {Object} obj
*/
isNumber:function(obj){
return toString.call(obj) == "[object Number]";
},
/**
* 是否为日期类型
* @return {Boolean}
* @param {Object} obj
*/
isDate:function(obj){
return toString.call(obj) == "[object Date]";
},
/**
* 是否为布尔类型
* @return {Boolean}
* @param {Object} obj
*/
isBoolean:function(obj){
return toString.call(obj) == "[object Boolean]";
},
/**
* 是否为字符串类型
* @return {Boolean}
* @param {Object} obj
*/
isString:function(obj){
return toString.call(obj) == "[object String]";
},
/**
* 是否为参数类型
* @return {Boolean}
* @param {Object} obj
*/
isArguments : function(obj) {
return toString.call(obj) == '[object Arguments]';
},
/**
* 是否为函数类型
* @return {Boolean}
* @param {Object} obj
*/
isFunction:function(obj){
return toString.call(obj) == "[object Function]";
},
/**
* 是否为正则表达式类型
* @return {Boolean}
* @param {Object} obj
*/
isRegExp:function(obj){
return toString.call(obj) == "[object RegExp]";
},
/**
* 是否为数组类型
* @return {Boolean}
* @param {Object} obj
*/
isArray:function(obj){
return toString.call(obj) == "[object Array]";
},
/**
* 是否为null
* @return {Boolean}
* @param {Object} obj
*/
isNull:function(obj){
return obj === null;
},
/**
* 是否为undefined
* @return {Boolean}
* @param {Object} obj
*/
isUndefined:function(obj){
return obj === undefined;
},
/**
* 是否为无限数值类型
* @return {Boolean}
* @param {Object} obj
*/
isFinite:function(obj){
return this.isNumber(obj) && isFinite(obj);
},
/**
* 是否为NaN
* NaN = Not-A-Number
*
* 在js中 条件判断 NaN == NaN 和 NaN === NaN 返回false,不能用等于号决定一个值是否为NaN
*
* 内置的isNaN(arg)函数,在处理arg时,会先将arg转换为number类型
*
* isNaN(NaN); // true
* isNaN(undefined); // true
* isNaN({}); // true
* isNaN(true); // false
* isNaN(null); // false
* isNaN(37); // false
// strings
* isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
* isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
* isNaN(""); // false: the empty string is converted to 0 which is not NaN
* isNaN(" "); // false: a string with spaces is converted to 0 which is not NaN
// This is a false positive and the reason why isNaN is not entirely reliable
* isNaN("blabla") // true: "blabla" is converted to a number. Parsing this as a number fails and returns NaN
* @return {Boolean}
* @param {Object} obj
* @reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
*/
isNaN:function(obj){
return obj !== obj;
},
/**
* 是否为类数组类型
* @return {Boolean}
* @param {Object} obj
*/
isArrayLike:function(obj){
return this.isObject(obj) && isNumber(obj.length);
},
/**
* 是否为可数值化类型
* @return {Boolean}
* @param {Object} obj
*/
isNumeric: function( obj ) {
return !isNaN( parseFloat(obj) ) && isFinite( obj );
},
/**
* 是否为可false
* js中可转换为false值的有
* null, 0, the empty string (""), undefined , NaN , false
* @return {Boolean}
* @param {Object} obj
*/
isFalseable:function(obj){
return Boolean(obj) === false ;
},
/**
* 是否为可true
* js中可转换为false值的有
* true || 非空字符串(Any nonempty string) || 任何非零数值(Any nonzero number , including infinity) || 任意对象(Any object,null除外)
* @return {Boolean}
* @param {Object} obj
*/
isTrueable:function(obj){
return Boolean(obj) === true ;
},
/**
* 是否为空字符串
* @return {Boolean}
* @param {Object} obj
*/
isEmptyString:function(obj){
return this.isString(obj) && /^\s*$/.test(obj) ;
},
/**
* 是否是空对象
* @return {Boolean}
* @param {Object} obj
*/
isEmptyObj:function(obj){
for ( var name in obj ) {
return false;
}
return true;
},
/**
*是否为DOM元素
* @return {Boolean}
* @param {HTMLElement} node
*/
isElement:function(node){
return node && node.nodeType == 1 ;
},
/**
* 是否是window对象
* @param obj
* @returns {Boolean}
*/
isWindow: function( obj ) {
return obj != null && obj == obj.window;
}
};
opt_ns = opt_ns || g_win;
opt_ns.lang = opt_ns.lang || lang ;
})(undefined , window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment