(obj instanceof ObjectClass)不工作?

时间:2016-10-17 20:52:18

标签: javascript class ecmascript-6 instanceof typechecking

鉴于课程:

let day = new Day()

我有day instanceof Day并通过几个函数调用传递它。 但后来我用console.log(day)检查了这个对象,它返回false。 当我在Chrome中Day {_moment: Moment, year: 2016, month: 9, day: 17}时,我得到day instanceof Day

在我使用instanceof检查之前,在控制台日志中使用Day前缀无法弄清楚为什么console.log('Day: ', day) console.log('Day: ', typeof day) console.log('Day: ', day instanceof Day) 返回false。

有什么想法吗?

编辑:

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

输出:

_id

1 个答案:

答案 0 :(得分:2)

@Bergi was correct, in one import of the Day class I wrote: import Day from '../lib/Day'

Webpack then generated another type of Day that was being used in that part of my codebase. Tip: always use lower case string on imports and require.