首页 > jshint的时候,grunt报错,说为一个变量为只读的,请大家指教。

jshint的时候,grunt报错,说为一个变量为只读的,请大家指教。

var getUserInfoFromC = function() {
        var current_user = [];
        current_user.id = 0;
        current_user.name = '';
        var cookie_user = document.cookie.match(".*_user=(.*)");
        if (cookie_user != null) {
            cookie_user_matched = cookie_user[1].split('%2C');
            current_user.id = cookie_user_matched[0];
            current_user.name = decodeURI(cookie_user_matched[1]);
        }
        return current_user;
    };
    


因为这里的cookie_user_matched变量为全变量,而且没有声明

jshint 會在設定上預設是認為全局變量為只讀(read-only),因為設置全局變量算是一件危險且容易污染環境
的行為,不過可以通過設置白名單來處理:

/*global cookie_user_matched:true */

就是因为这是一个全局变量。
文件一开始写个

/*global cookie_user_matched:true */

试试

【热门文章】
【热门文章】