您的位置:首页 > Web前端 > JavaScript

javascript工具函数汇总2013

2013-11-16 20:29 267 查看
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}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: