Bug能藏多长时间? 如果一直没人注意,也许会一直生存下去…… 下面是一个设置cookie的函数, 今天才发现它居然有这么严重的bug,你能看出来吗?
function setCookie (name, value) { //设置名称为name,值为value的Cookie
var argc = arguments.length;
var argv = arguments;
var path = (argc >= 2) ? argv[2] : '/';
var domain = (argc >= 3) ? argv[3] : null;
var secure = (argc >= 4) ? argv[4] : false;
str = name + "=" + value +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
document.cookie = str;
}
function getCookie(c_name) {
if (document.cookie.length>0) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";",c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
问题在这里,绕了半天才想明白。 var argc = arguments.length; var argv = arguments; var path = (argc >= 2) ? argv[2] : ‘/’; var domain = (argc >= 3) ? argv[3] : null; var secure = (argc >= 4) ? argv[4] : false; 因为argc的取值是1,2,3… argv[]的key取值是0,1,2,3…..