zoukankan      html  css  js  c++  java
  • JS-实用方法整理

    // 深拷贝

     function deepClone(obj) {
            if (obj == null || typeof obj !== 'object') {
                return obj;
            }
            switch (Object.prototype.toString.call(obj)) {
                case '[object Array]':
                    {
                        var result = new Array(obj.length);
                        for (var i = 0; i < result.length; ++i) {
                            result[i] = deepClone(obj[i]);
                        }
                        return result;
                    }
    
                case '[object Error]':
                    {
                        var result = new obj.constructor(obj.message);
                        result.stack = obj.stack; // hack...
                        return result;
                    }
    
                case '[object Date]':
                case '[object RegExp]':
                case '[object Int8Array]':
                case '[object Uint8Array]':
                case '[object Uint8ClampedArray]':
                case '[object Int16Array]':
                case '[object Uint16Array]':
                case '[object Int32Array]':
                case '[object Uint32Array]':
                case '[object Float32Array]':
                case '[object Float64Array]':
                case '[object Map]':
                case '[object Set]':
                    return new obj.constructor(obj);
    
                case '[object Object]':
                    {
                        var keys = Object.keys(obj);
                        var result = {};
                        for (var i = 0; i < keys.length; ++i) {
                            var key = keys[i];
                            result[key] = deepClone(obj[key]);
                        }
                        return result;
                    }
    
                default:
                    {
                        throw new Error("Unable to copy obj! Its type isn't supported.");
                    }
            }
        }

    // 判断类型

    function checkType(obj) {
        return Object.prototype.toString.call(obj);
    }
    console.log(checkType(1)); //[object Number]
    console.log(checkType('1')); //[object String]
    console.log(checkType(true)); //[object Boolean]
    console.log(checkType(undefined)); //[object Undefined]
    console.log(checkType(null)); //[object Null]
    console.log(checkType([1, 2, 3])); //[object Array]
    console.log(checkType({ a: 1, b: 2 })); //[object Object]
    console.log(checkType(function() {})); //[object Function]
     

    //随机字符串

    function randomString(length) {
        var type = Object.prototype.toString.call(length);
        if (type === '[object Number]' || type === '[object Undefined]') {
            length = length || 18;
            var random_number = new Date().getTime().toString(36); 
            var base_charts = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
            var chart_length = base_charts.length;
            var str = '';
            for (var i = 0; i < length; i++) {    
                str += base_charts.charAt(Math.floor(Math.random() * chart_length));  
            } 
            if (length <= 8) {
                return str;
            } else {
                return str.substr(0, str.length - 8) + random_number;
            }
        } else {
            alert('输入错误')
        }
    }
  • 相关阅读:
    MyISAM表锁的解决方案
    RSA数字证书管理
    Self Host WebApi服务传输层SSL加密(服务器端+客户端调用)
    WebApi服务Uri加密及验证的两种方式
    利用MVC的自定义过滤器FilterAttribute、IActionFilter、IExceptionFilter实现异常处理等功能
    html页面中meta的作用
    [转]REST简介
    [转]webApi 参数传递总结
    REST服务中的异常处理
    REST服务返回自定义的HttpResponseMessage
  • 原文地址:https://www.cnblogs.com/xbgTitle/p/8744405.html
Copyright © 2011-2022 走看看