zoukankan      html  css  js  c++  java
  • 一些常用的JS方法

    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的。

    保持一颗好奇的心
  • 相关阅读:
    2019天梯赛训练1
    Python课程设计 搭建博客
    最容易理解的贪吃蛇小游戏
    数据结构-队列
    数据结构-堆栈(2)
    数据结构-堆栈(1)
    数据结构-线性表(3)
    数据结构-线性表(1)
    linux知识积累
    Maven学习笔记
  • 原文地址:https://www.cnblogs.com/subying/p/3063192.html
Copyright © 2011-2022 走看看