-3

这是我的功能:-

function setLangCookie() {
  "use strict";
    var value = "FR";
    var expiredays = 1825;
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = "lang=" + escape(value) + ((expiredays == null) ? "" : ";path=/;expires=" + exdate.toGMTString());
}

错误:

第 7 行:document.cookie = "lang=" + escape(value) + ((expiredays == null) ? "" : ";path=/;expires=" + exdate.toGMTString());

期待'===',反而看到了'=='

第 7 行:document.cookie = "lang=" + escape(value) + ((expiredays == null) ? "" : ";path=/;expires=" + exdate.toGMTString());

'escape'没有定义。

4

3 回答 3

3

你在strict mode。您必须使用前缀访问窗口对象的属性window.

于 2012-11-02T13:13:43.350 回答
1

它希望您使用window.escape而不仅仅是escape.

如果您使用默认设置,我还假设它会因为您不使用带有空值检查的三等号而对您大喊大叫。

function setLangCookie() {
  "use strict";
    var value = "FR";
    var expiredays = 1825;
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = "lang=" + window.escape(value) + ((expiredays === null) ? "" : ";path=/;expires=" + exdate.toGMTString());
}
于 2012-11-02T13:14:57.103 回答
0

Aside from escape not being defined, the complaint about seeing '==' instead of '===' relates to the way that Javascript treats equality. '===' requires the two compared values to be of the same type, where '==' will do type coercion to get equality.

In most cases you really want to use '===' instead because it prevents subtle bugs, such as 0 being treated as false.

于 2012-11-02T13:18:25.223 回答