JS-實用方法整理

杉VS冉發表於2018-04-08

// 深拷貝

 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(`輸入錯誤`)
    }
}

 

相關文章