迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中 TypeError: toISOString is not a function 错误

作者:迹忆客 最近更新:2023/01/20 浏览次数:

当对不是日期对象的值调用 toISOString() 方法时,会发生“TypeError: toISOString is not a function”错误。

要解决此错误,需要在调用方法之前将值转换为日期,或确保仅对有效日期对象调用 toISOString() 方法。

下面是产生上述错误的示例代码

const d = Date.now();
console.log(d); // 👉️ 1639....

// ⛔️ TypeError: toISOString is not a function
const result = d.toISOString();

JavaScript 中 TypeError- toISOString is not a function 错误

Date.now() 函数返回一个整数,因此对整数调用 Date.toISOString() 方法会导致错误。

仅对有效的 Date 对象调用 toISOString() 方法

要解决该错误,需要确保仅对有效的 Date 对象调用 toISOString() 方法。

// ✅ Works
const d1 = new Date().toISOString();
console.log(d1);

const d2 = new Date('Sept 24, 22 13:20:18').toISOString();
console.log(d2); // 👉️ 2022-09-24...

我们可以通过将有效日期传递给 Date() 构造函数来获取 Date 对象。

请注意 ,如果将无效日期传递给 Date() 构造函数,您将得到“Invalid time value”RangeError。

// ⛔️ RangeError: invalid time value
const d1 = new Date('invalid').toISOString();
console.log(d1);

我们可以使用 console.log 打印正在调用 toISOString 方法的值,以检查它是否是有效的 Date 对象。

检查该值是否是有效的 Date 对象

我们还可以有条件地检查该值是否为 Date 对象。

const d1 = new Date();

if (typeof d1 === 'object' && d1 !== null && 'toISOString' in d1) {
  const result = d1.toISOString();
  console.log(result);
}

我们的 if 语句使用逻辑与 && 运算符,因此要运行 if 块,必须满足所有条件。

我们首先检查 d1 变量是否存储了具有对象类型的值,因为日期具有对象类型。

然后我们检查变量是否不等于 null。 不幸的是,如果我们使用 console.log(typeof null),我们将得到一个“object”值,因此我们必须确保该值不为空。

console.log(typeof null); // 👉️ "object"

我们检查的最后一件事是该对象包含 toISOString 属性。

然后我们知道我们可以安全地对该对象调用 toISOString 方法。

这种方法称为 duck-typing 打字。

使用 duck-typing 时,我们只需检查对象是否实现了特定的属性或方法,如果实现了,我们就假定它是正确类型的对象。


总结

当对不是日期对象的值调用 toISOString() 方法时,会发生“TypeError: toISOString is not a function”错误。

要解决此错误,请在调用方法之前将值转换为日期,或确保仅对有效日期对象调用 toISOString() 方法。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便