javascript工具函数汇总2013

来源:互联网 发布:uboot usb启动linux 编辑:程序博客网 时间:2024/06/07 06:28
function int(str){    return parseInt(str, 10);}/* * 判断目标参数是否Array对象 */function isArray(source){    return '[object Array]' == Object.prototype.toString.call(source);}/* * 解析目标URL中的参数成json对象 */function queryToJson(url){     var getParams = url.substr(url.indexOf('?') + 1),        item = getParams.split('&'),        map = {},key,value,t;    for(var i=0; i<item.length; i++){        if(!item[i]){            continue;        }        key = item[i].split('=')[0];        value = item[i].split('=')[1];        t = map[key];        if('undefined' == typeof t){            map[key] = value;        }else if(isArray(t)){            t.push(value);        }else{            map[key] = [t,value];        }    }    console.log(map);}var url = "http://baidu.com?kwd=ade&action=suggest&kwd=op";queryToJson(url);/** * 将源对象的所有属性拷贝到目标对象中 * 源对象的prototype成员不会拷贝 */function extend(target,source){    for(p in source){if(source.hasOwnProperty(p)){target[p] = source[p];}}return target;}var d = extend({a:1},{a:2,b:3});console.log(d); //{a:2,b:3}

原创粉丝点击