First of all, instanceof
isn't perfectly reliable.
Second of all, moment()
returns instance of Moment
class that isn't exposed to user. Following code prove this:
moment().__proto__.constructor // function Moment()
moment().constructor === moment; // false
Third of all, moment
provide function moment.isMoment
that will solve your problem.
And last, but not least - your code should use consistent return types - always return moment
instances or always return strings. It will reduce your pain in future.
You can ensure that you always have moment
instance by calling moment
function - moment(string)
equals in value moment(moment(string))
, so you can just always convert your argument to moment
instance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…