JavaScript插件-无法读取属性';窗口';未定义的

JavaScript插件-无法读取属性';窗口';未定义的,javascript,Javascript,我正在尝试编写vanilla JavaScript插件,但出现以下错误: cannot read property 'window' of undefined 这是我的代码: (function (root, factory) { if ( typeof define === 'function' && define.amd ) { define([], factory(root)); } else if ( typeof exports === 'objec

我正在尝试编写vanilla JavaScript插件,但出现以下错误:

cannot read property 'window' of undefined 
这是我的代码:

(function (root, factory) {
  if ( typeof define === 'function' && define.amd ) {
    define([], factory(root));
  } else if ( typeof exports === 'object' ) {
    module.exports = factory(root);
  } else {
    root.myPlugin = factory(root);
  }
})(typeof global !== "undefined" ? global : this.window || this.global, function (root) {
  'use strict';
...

如果你想获得真正的乐趣,你可以把你的长队改成

(typeof global !== 'undefined' ? global : 
        typeof self !== 'undefined' ? self :
        typeof window !== 'undefined' ? window : {}, function (root) {
    // code
});
但像Patrick建议的那样学习scope对您来说总是更好的