JS-实用方法整理

2018-06-24 01:51:29来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

// 深拷贝

 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('输入错误')
    }
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:node学习日志(一)--Node.js设计核心理念

下一篇:node学习日志(二)---核心API