我写了一段代码来解决 setTimeout()
函数中的 this
关键字问题。并尝试在 Node 中运行它并显示 throw err;找不到模块错误。然后我尝试在浏览器中运行它并且成功了。我的意思是如何知道何时在 nodejs 中测试我们的代码,何时不。这是我的代码
function person () {
var firstName ;
var _this_ = this;
return {
saveContext: function(context) {
_this_ = context;
},
setName: function(name) {
_this_.firstName = name;
},
getName: function() {
console.log(_this_.firstName);
}
};
}
var employee1 = new person();
employee1.saveContext(employee1);
employee1.setName('Steve');
employee1.getName();
setTimeout(employee1.getName, 1000);
最佳答案
“找不到模块”错误在以下情况下抛出:
您尝试在一个不存在的文件上运行 Node 。
文件存在,但不在该目录中。
您需要一个不存在的文件。
关于Javascript 代码在 Node 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452577/