[英]TypeError: Cannot set property of undefined in module.exports
'use strict';
module.exports = function () {
this.myVar = 'example';
console.log(this.myVar);
};
I get the error: 我得到错误:
/home/karl/mymodule.js:3
this.myVar = 'example';
^
TypeError: Cannot set property 'myVar' of undefined
In strict mode this
is undefined
. 在严格模式下,
this
是undefined
。 But in non-strict mode this
is nothing but pointing to GLOBAL
. 但是在非严格模式下,
this
只不过指向GLOBAL
。 So here you are trying to set something to undefined
. 因此,在这里您尝试将某些内容设置为
undefined
。
Where exactly were you intending to attach myVar
? 您打算将
myVar
固定在哪里? On your module
? 在您的
module
? Or on GLOBAL
? 还是在
GLOBAL
? If it's on GLOBAL
(which is actually a bad idea), use GLOBAL.myVar
or global.myVar
. 如果在
GLOBAL
(这实际上是个坏主意),请使用GLOBAL.myVar
或global.myVar
。 If it's on your module you could do module.exports.myVar = 'example';
如果在您的模块上,则可以执行
module.exports.myVar = 'example';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.