您的位置:首页 > Web前端 > JavaScript

一些常用的JS方法

2013-05-06 17:27 465 查看
var oap={},obj_type={},core_toString=obj_type.toString,hasOwn = obj_type.hasOwnProperty;;
var type_arr = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
for(var i in type_arr){
obj_type[ "[object " + type_arr[i] + "]" ] = type_arr[i].toLowerCase();
};

//常用工具  判断方法 对象
oap={
type:function(obj){
if ( obj == null ) {
return String( obj );
}
return (typeof obj === "object" || typeof obj === "function")?(obj_type[ core_toString.call(obj) ] || "object") :typeof obj;
},
isStr:function(obj){
return oap.type(obj)==="string";
},
isFn:function(obj){
return oap.type(obj)==="function";
},
isObj:function(obj){
return oap.type(obj) === "object";
},
isDate:function(obj){
return oap.type(obj) === "date";
},
isEmptyObj:function(obj){
var name;
for ( name in obj ) {
return false;
}
return true;
},
isNum:function(obj){
return !isNaN( parseFloat(obj) ) && isFinite( obj );
},
isArr:function(obj){
return oap.type(obj) === "array";
},
trim:function(obj){
return obj.replace(/^\s+|\s+$/g, "");
},
now:function(){
return new Date().getTime();
},
log:function(str){
if(console && console.log){
console.log(str);
}
}
};


自己写的一段代码,主要是一些常用的判断方法,其中的type是模仿了jquery的。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: