2017-07-19 12 views
0

我想写香草的JavaScript插件属性“窗口”,但越来越以下错误: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'; 
... 
+0

[请参阅'this' MDN]中的此部分(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#Simple_call) –

回答

0

如果你想得到真正的幻想,你可以改变你的长线,像

(typeof global !== 'undefined' ? global : 
     typeof self !== 'undefined' ? self : 
     typeof window !== 'undefined' ? window : {}, function (root) { 
    // code 
}); 

但它总是bett呃让你学习像帕特里克建议的范围。