我正在尝试理解 JavaScript 世界中的类型。我的页面正在使用 moment.js .我有一个函数,有时会返回一个 moment()
,有时会返回一个 string
(它是遗留代码)。
我的代码看起来像这样:
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;
}
当我执行上面的代码时,我从来没有得到 we have a moment.
。即使我手动设置 result = moment();
。这是为什么?我是不是误解了 instanceof
或 moment
?
最佳答案
关于JavaScript instanceof 和 moment.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921026/