javascript 和 jshint,未定义

javascript and jshint, undefined

今天我写了这段代码:

(function (window) {
    'use strict';

    function ViewPort() {
        var getSize = function () {
                var e = window,
                    a = 'inner';

                if (!('innerWidth' in window)) {
                    a = 'client';
                    e = Document.documentElement || Document.body;
                }
                return { width : e[a + 'Width'], height : e[a + 'Height'] };
            },

            update = function () {
                var vw = (getSize().width / 100);

                Document.querySelector('html').style.fontSize = vw + 'px';
            };

        Document.addEventListener("resize", update());
    }

    function run() {
        return new ViewPort();
    }

    window.viewport = run;
}(window));

window.onload = function () {
    'use strict';
    viewport();
};

当我使用 jshint 时,出现如下错误:

11  'Document' is not defined. (W117)   e = Document.documentElement || Document.body;
19  'Document' is not defined. (W117)   Document.querySelector('html').style.fontSize = vw + 'px';
22  'Document' is not defined. (W117)   Document.addEventListener("resize", update());
30  'window' is not defined. (W117) }(window));
32  'window' is not defined. (W117) window.onload = function () {
34  'viewport' is not defined. (W117)   viewport();

谁能帮我解决我的错误?我不知道如何修复它,我在 scripts.js 文件中的这段代码不是内联的。

Javascript 区分大小写。 document 全部小写。那么它应该可以工作。

如果仍然无效,您可以尝试在文件开头添加一个指令,以暗示该脚本适用于浏览器,并且浏览器全局变量可用。

将此评论添加到脚本顶部:

/* jshint browser: true */