对理解这个设置 cookie 的功能帮助不大?

little help understanding this set cookie function?

排行新手问题(见谅)--如果有更好的论坛,请告诉我。我想设置一个三十天后过期的 cookie。我在 Whosebug 上找到了回复,并查看了几个关于如何设置 cookie 的在线解释,但我无法完全理解我所看到的内容。这里是 the answer given on Whosebug:

function createCookie(name, value, days) {
    var date, expires;
    if (days) {
        date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

如果我错了(请),你可以在这里纠正我:

第 1 行创建了一个名为 createCookie 的函数,它具有三个参数,namevaluedays。当用 createCookie(testCookie, ?, 30); 之类的东西调用函数时,会定义或设置这些参数。我在参数 value 中打了一个问号,因为我不确定那里是什么。

第 2 行建立了两个变量,dateexpires,但没有为这些变量赋值。

第 3 行说如果参数 days 然后直接执行一些行。这超出了我对 JS 的理解,到目前为止我已经完成了 if (x > 10) { 之类的事情,其中​​ x > 10 定义了一个条件,当为真时,将执行以下括号中的代码,如果不是,则跳到if/else 的其他部分。 days 不是我理解的条件。我想我对这部分的理解不是最重要的,只要它有效。

第 4 行创建一个名为 date 的变量并将当前日期分配给它。

第 5 行 date.setTime(date.getTime()+(days*24*60*60*1000)); 使用函数的参数 days 进行数学计算得出实际到期日期并将其分配给变量 date.

第 6 行 expires = "; expires="+date.toGMTString(); 为变量 expires 赋值,但我不太理解,因为看起来该值是 expires 的串联,它在那一点,加上参数date的值表示为字符串?另外,现在 toGMTString() 似乎已被弃用?同样,我对此的理解可能不是最重要的;但是,我的缺乏增加了我的整体困惑。

第8行用name加上字符串"="加上value加上expires加上字符串"; path=/"设置cookie。我还是不知道 value 应该是什么...

有人想帮我理解这个吗?谢谢!

第 1 行 - value 是您要存储在 cookie 中的数据。

createCookie("testCookie", "The value I want to store", 30);

第 3 行 - 这是一个真实的检查,它正在检查以确保没有未定义的天数。

第 6 行 - 我认为您混淆了变量过期的字符串 "expires"。对于 GMT,可能是在它被弃用之前编写的。

function createCookie(name, value, days) { // this passes in the values for the cookie you want to store
    var date, expires; // set the variables
    if (days) { // check to make sure days is not undefined
        date = new Date(); // sets todays date
        date.setTime(date.getTime()+(days*24*60*60*1000)); // does maths to make it expiry after `day` parameter you passed into the function.
        expires = "; expires="+date.toGMTString(); // sets the expiry date
    } else {
        expires = ""; // else if days is undefined sets nothing to expiry
    }
    document.cookie = name+"="+value+expires+"; path=/"; // sets the cookie.
}