给定类(class):
class Day {
constructor(year, month, day) {
this._moment = new Moment()
this.year = year || this._moment.year()
this.month = month || this._moment.month()
this.day = day || this._moment.date()
}
}
我有 let day = new Day()
并通过几个函数调用传递它。
但后来我用 day instanceof Day 检查这个对象,它返回 false。
当我在 chrome 中 console.log(day)
时,我得到 Day {_moment: Moment,year:2016,month:9,day:17}
。
在我检查 instanceof 之前,控制台日志中有 Day 前缀,无法弄清楚为什么 day instanceof Day
返回 false。
有什么想法吗?
编辑:
console.log('Day: ', day)
console.log('Day: ', typeof day)
console.log('Day: ', day instanceof Day)
输出:
Day: Day {_moment: Moment, year: 2016, month: 9, day: 17}
_moment: Moment
day: 17
month: 9
year: 2016
__proto__: Object(anonymous function)
Day: object
Day: false
最佳答案
@Bergi 是正确的,在 Day 类(class)的一次导入中我写道: 从 '../lib/Day' 导入 Day
然后,Webpack 生成了另一种类型的 Day,并在我的代码库的该部分中使用。提示:在导入和 require 中始终使用小写字符串。
关于javascript - (obj instanceof ObjectClass) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40095452/