javascriptツール関数のまとめ2013


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}