Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript instanceof and moment.js

I'm attempting to understand types in the JavaScript world. My page is using moment.js. I have a function that sometimes returns a moment() and other times, returns a string (it's legacy code gone wild).

My code kind of looks like this:

var now = getDate(); if (now instanceof moment) {   console.log('we have a moment.'); } else {   console.log('we have a string.'); }   function getDate() {   var result = null;   // Sometimes result will be a moment(), other times, result will be a string.   result = moment();   return result; } 

When I execute the code above, I never get we have a moment.. Even if I manually make set result = moment();. Why is that? Am I misunderstanding instanceof or moment?

like image 381
Some User Avatar asked Aug 10 '15 13:08

Some User


1 Answers

Moment version 2.13.0

There is a static method .isMoment:

enter image description here

like image 146
FreeLightman Avatar answered Sep 21 '22 05:09

FreeLightman