修复 JavaScript 错误的四种方法

开发 前端
修复 JavaScript 中“无法读取 Undefined 的属性‘push’”错误的 4 种方法

修复 JavaScript 中“无法读取 Undefined 的属性‘push’”错误的 4 种方法

了解如何轻松修复 JavaScript 中的“无法读取未定义的属性‘push’”错误。

当您尝试对旨在包含数组但实际上包含未定义值的变量调用 push() 方法时,会出现 JavaScript 中的“无法读取未定义的属性‘push’”错误。

这可能是由多种原因引起的:

  1. 对变量调用 push() 方法,而无需先使用数组对其进行初始化。
  2. 对数组元素而不是数组本身调用 push() 方法。
  3. 对先前设置为未定义的变量调用 push() 方法。
  4. 对不存在或值为 undefined 的对象属性调用 push() 方法。

我们将在本文中探讨所有这些可能原因的实用解决方案。

1. 在变量上调用 push() 方法,而不用先用数组初始化它

要修复“无法读取未定义的属性‘push’”错误,请确保变量在调用 push() 方法之前已使用数组初始化。

let doubles;let nums = [1, 2, 3, 4, 5];for (const num of nums) {
let double = num * 2; // ❌ TypeError: cannot read properties of undefined (reading 'push')
doubles.push(double);
}console.log(doubles);

在上面的例子中,我们在 doubles 变量上调用了 push() 方法,而没有先初始化它。

let doubles;console.log(doubles); // undefined

因为未初始化的变量在 JavaScript 中具有 undefined 的默认值,所以调用 push() 会引发错误。

要修复该错误,我们所要做的就是将 doubles 变量分配给一个数组(对于我们的用例来说是空的):

// ✅ "doubles" initialized before use
let doubles = [];let nums = [1, 2, 3, 4, 5];for (const num of nums) {
let double = num * 2; // ✅ push() called - no error thrown
doubles.push(double);
}console.log(doubles); // [ 2, 4, 6, 8, 10 ]

2. 对数组元素而不是数组本身调用 push() 方法

要修复“无法读取未定义的属性‘push’”错误,请确保在调用 push() 之前没有从数组变量中访问元素,而是在实际数组变量上调用 push()。

const array = [];// ❌ TypeError: Cannot read properties of undefined (reading 'push')
array[0].push('html');
array[0].push('css');
array[0].push('javascript');console.log(array);

使用括号索引访问 0 属性为我们提供了数组索引 0 处的元素。 该数组没有元素,因此 arr[0] 的计算结果为 undefined 并对其调用 push() 会导致错误。

为了解决这个问题,我们需要在数组变量上调用 push,而不是它的元素之一。

const array = [];// ✅ Call push() on "array" variable, not "array[0]"
array.push('html');
array.push('css');
array.push('javascript');console.log(array); // [ 'html', 'css', 'javascript' ]

3. 对之前设置为 undefined 的变量调用 push() 方法

要修复“无法读取未定义的属性‘push’”错误,请确保最后分配给变量的值不是未定义的。

let arr = ['orange'];arr.push('watermelon');arr = undefined;// hundreds of lines of code...// ❌ TypeError: Cannot read properties of undefined (reading 'push')
arr.push('apple');console.log(arr);

这里的问题是我们曾经明确地将变量设置为未定义,但后来我们在变量上调用了 push()。 对此的修复将根据您的情况而有所不同。

也许您在再次使用之前忘记将变量设置为定义的值:

let arr = ['orange'];arr.push('watermelon');// ✅ Fixed: variable re-assigned
arr = ['banana'];// hundreds of lines of code...arr.push('apple');console.log(arr); // [ 'banana', 'apple' ]

或者,也许你犯了一个错误,根本不打算将变量分配给 undefined:

let arr = ['orange'];arr.push('watermelon');// arr = undefined; ✅ Fixed: line removedarr.push('apple');console.log(arr); // [ 'orange', 'watermelon', 'apple' ]

4. 对不存在或值为 undefined 的对象属性调用 push() 方法

要修复 JavaScript 中的“无法读取未定义的属性‘push’”错误,请确保调用 push() 方法的对象属性存在且未定义。

const students = [
{ name: 'Mac', scores: [80, 85] },
{ name: 'Robert' },
{ name: 'Michael', scores: [90, 70] },
];// ❌ TypeError: Cannot read properties of undefined (reading 'push')
students[1].scores.push(50);

在这种情况下,出现错误是因为索引 1 处的对象元素没有 score 属性。

const obj = {};console.log(obj.prop); // undefined

从对象访问不存在的属性不会在 JavaScript 中引发错误,而是会为您提供 undefined 值。 如果您尝试在该不存在的属性上调用类似 push() 的方法,您将遇到错误。

在这种情况下,我们可以通过将第二个数组元素的 score 属性设置为定义的值来修复错误。

const students = [
{ name: 'Mac', scores: [80, 85] },
// ✅ Fixed: "scores" set to a defined value
{ name: 'Robert', scores: [] },
{ name: 'Michael', scores: [90, 70] },
];// ✅ "scores" property exists, "push()" works - no error thrown
students[1].scores.push(50);
责任编辑:华轩 来源: 今日头条
相关推荐

2009-11-23 15:57:51

PHP伪静态

2021-03-10 10:13:39

爬虫Python代码

2014-03-17 09:22:43

Linux命令

2022-11-04 13:35:29

IT远程工作混合工作

2022-12-07 10:28:22

2009-03-31 13:12:30

解析XMLJava

2009-02-25 09:52:14

类型转换.NET 强制转型

2020-08-10 00:30:55

备份密码iPhone移动安全

2011-06-22 15:21:08

XML

2016-06-28 10:19:31

云计算云安全

2010-07-16 13:50:53

Perl哈希表

2023-06-16 15:17:21

sprint工具

2011-04-21 10:13:27

升级Ubuntu

2011-07-06 18:07:16

ASP

2023-07-31 10:09:03

CIOIT领导者

2023-03-17 10:51:26

2010-08-31 15:51:51

DB2清除数据

2010-08-02 16:47:46

Flex

2009-09-17 16:55:58

C#组件设计

2010-03-18 17:57:37

Java XMLSoc
点赞
收藏

51CTO技术栈公众号