js 综合

1、实现一个克隆方法

function clone(temp) {
    var globalTemp;
    switch (typeof temp) {
        case 'null':
            globalTemp = temp;
            break;
        case 'string':
            globalTemp = temp;
            break;
        case 'number':
            globalTemp = temp;
            break;
        case 'undefined':
            globalTemp = temp;
            break;
        case "boolean":
            globalTemp = temp;
            break;
        case 'object':
            if (temp instanceof Array) {
                globalTemp = [];
                for (var i = 0; i < temp.length; i++) {
                    globalTemp.push(clone(temp[i]));
                }
            } else {
                globalTemp = {};
                for(var key in temp) {
                    globalTemp[key] = clone(temp[key]);
                }
            }
            break;
        default: break;
    }
    return globalTemp;
}

2、数组去重

function clearArr(arr) {
    var arr2 = [];
    if (arr instanceof Array) {
        for(var i = 0,len = arr.length; i< len; i++){
            if (arr2.indexOf(arr[i]) < 0)  {
                arr2.push(arr[i]);
            }
        }
        return arr2;
    } else {
        return false;
    }
}
相关文章