account_disabled
New Neighbor
member is offline
|
Post by account_disabled on Dec 11, 2023 4:25:22 GMT
因此从技术上讲,这些陈述是完全正常的: 但是,转换原始数据类型不会提供有关错误的详细信息,例如类型、名称或随附的堆栈跟踪。为了解决这个问题并标准化错误处理流程, 个最常见的 JavaScript 错误 现在您已经了解了常见错误的类型以及如何创建自定义错误,现在是时候看看编写 JavaScript 代码时会遇到的一些最常见错误了。 查看我们的最常见 JavaScript 错误视频指南 1. 未捕获的范围错误 Google Chrome 在不同情况下都会出现此错误。首先,如果您调用递归函数并且它没有终止,则可能会发生这种情况。您可以在 Chrome DevTools 中亲自查看: “未捕获的范围错误:超出最大调用堆栈大小”错误显示在红色背景上,旁边是红叉图标,上面有递归函数代码 具有递归函数调用的 RangeError 示例。 因此要解决此错误,请确保正确定义递归 电话号码清单 函数的边缘情况。发生此错误的另一个原因是您传递的值超出了函数参数的范围。这是一个例子: 错误“未捕获的 参数必须介于 0 和 100 之间”显示在红色背景上,旁边是红叉图标,上方有函数调用 toExponential()。 使用 toExponential() 调用的 RangeError 示例。 错误消息通常表明您的代码有什么问题。一旦进行更改,它将被修复。 输出 函数调用的输出。 2. 未捕获的类型错误:无法设置属性 当您在未定义的引用上设置属性时,会发生此错误。您可以使用以下代码重现该问题:下是您将收到的输出: “未捕获的类型错误:无法设置未定义的属性”错误显示在红色十字图标旁边的红色背景上,上面有 赋值 类型错误示例。 要解决此错误,请在访问其属性之前使用值初始化引用。这是解决后的样子: 使用 {} 初始化列表后设置 输出为 10。 如何修复类型错误。 3. 未捕获的类型错误:无法读取属性 这是 JavaScript 中最常见的错误之一:当您尝试读取未定义对象的属性或调用函数时,就会发生这种情况。您可以通过在 Chrome 开发者控制台中运行以下代码来轻松重现这一点 这是输出: “未捕获的类型错误:无法读取未定义的属性”错误显示在红色背景上,旁边是红叉图标,上面。 具有未定义函数的 TypeError 示例。 未定义的对象是导致此错误的众多可能原因之一。导致此问题的另一个重要原因可能是渲染 UI 时状态初始化不当。这是一个 React 应用程序的真实示例 应用程序以空状态容器启动,并在 2 秒延迟后接收一些项目。延迟是为了模仿网络呼叫而设置的。
|
|