面试官让手写一个解析查询字符串的方法

137 阅读1分钟

function getQueryArgs(url) {

var qs = (url.length > 0 ? url.substring(url.indexOf('?')).substr(1) : ''),

//保存每一项

args = {},

//得到每一项

items = qs.length ? qs.split('&') : [],

item = null,

name = null,

value = null,

i = 0,

len = items.length;

for (i = 0; i < len; i++) {

// eslint-disable-next-line no-unused-expressions

item = items[i].split('='),

name = decodeURIComponent(item[0])

value = decodeURIComponent(item[1])

if (name.length) {

args[name] = value;

}

}

return args;

}

讲了下思路,面试官说不行,需要手写出来,给个电脑让现场写,没写出来,

然后因为这个没过面试吧,

不想说什么 ……累………………