简体   繁体   English

IE11中未定义“文档”

[英]'Doc' is undefined in IE11

I have an issue with my sample website which gave me an error: Doc is undefined showing in dev tools which happens at this line: Doc.Show.init(); 我的示例网站出现问题,给我一个错误:Doc在此行发生在开发工具中时是未定义的:Doc.Show.init();

Any suggestion why it did not work in IE11 but worked in chrome and firefox 任何建议,为什么它在IE11中不起作用,但在chrome和firefox中起作用

Thanks in advance. 提前致谢。

 'use strict' var Doc = Doc || {}; Doc.Show = function() { let init = function init() { console.log("nothing to show"); }; return { init: init }; }(); 
 h1 { color: red; text-align: center; } blockquote::before { content: open-quote; } blockquote::after { content: close-quote; } 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="/css/style.css"> <script src="/script/doc.js"></script> <script> Doc.Show.init(); </script> </head> <body> <h1>This is Header one</h1> <blockquote>This is blockquote</blockquote> </body> </html> 

The problem is this line: 问题是这一行:

var Doc = Doc || {};

You're trying to use the global variable Doc in the initialization of Doc . 您正在尝试在Doc的初始化中使用全局变量Doc This gets an error if the global variable doesn't already exist, because strict mode won't automatically create global variables. 如果全局变量尚不存在,则会出现错误,因为严格模式不会自动创建全局变量。

Strict mode also doesn't allow creating global variables by declaring them at top level. 严格模式也不允许通过在顶层声明全局变量来创建全局变量。 You have to create them as window properties. 您必须将它们创建为window属性。

window.Doc = window.Doc || {};

window.Doc.Show = function() {

    let init = function init() {
        console.log("nothing to show");
    };

    return {
        init: init
    };
}();

And use window when calling it: 并在调用它时使用window

window.Doc.Show.init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM