我是 Node 模块的新手。我试过了
module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + ' ' + this.lastName;
}
}
然后
让 person1 = new person('Rajesh','Dhoundiyal')
这有效。但是当我尝试在 module.exports 中使用 Fat arrow 时,它不起作用。
例如module.exports = (firstName, lastName) => {
稍后会出现错误 person is not a constructor
。我不知道为什么会发生这种情况。谁能告诉我为什么胖箭头在这里不起作用。
最佳答案
this
在arrow-funciton
与其他正常功能不一样。 this
在arrow-funciton
指向 arrow-funciton
的上下文被定义,所以它不能用作构造函数
请引用Arrow functions :
An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target.These function expressions are best suited for non-method functions, and they cannot be used as constructors.
关于javascript - 在 Node 模块 module.exports 中使用胖箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46700146/