自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

修復 JavaScript 錯誤的四種方法

開發(fā) 前端
修復 JavaScript 中“無法讀取 Undefined 的屬性‘push’”錯誤的 4 種方法

修復 JavaScript 中“無法讀取 Undefined 的屬性‘push’”錯誤的 4 種方法

了解如何輕松修復 JavaScript 中的“無法讀取未定義的屬性‘push’”錯誤。

當您嘗試對旨在包含數(shù)組但實際上包含未定義值的變量調(diào)用 push() 方法時,會出現(xiàn) JavaScript 中的“無法讀取未定義的屬性‘push’”錯誤。

這可能是由多種原因引起的:

  1. 對變量調(diào)用 push() 方法,而無需先使用數(shù)組對其進行初始化。
  2. 對數(shù)組元素而不是數(shù)組本身調(diào)用 push() 方法。
  3. 對先前設置為未定義的變量調(diào)用 push() 方法。
  4. 對不存在或值為 undefined 的對象屬性調(diào)用 push() 方法。

我們將在本文中探討所有這些可能原因的實用解決方案。

1. 在變量上調(diào)用 push() 方法,而不用先用數(shù)組初始化它

要修復“無法讀取未定義的屬性‘push’”錯誤,請確保變量在調(diào)用 push() 方法之前已使用數(shù)組初始化。

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 變量上調(diào)用了 push() 方法,而沒有先初始化它。

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

因為未初始化的變量在 JavaScript 中具有 undefined 的默認值,所以調(diào)用 push() 會引發(fā)錯誤。

要修復該錯誤,我們所要做的就是將 doubles 變量分配給一個數(shù)組(對于我們的用例來說是空的):

// ? "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. 對數(shù)組元素而不是數(shù)組本身調(diào)用 push() 方法

要修復“無法讀取未定義的屬性‘push’”錯誤,請確保在調(diào)用 push() 之前沒有從數(shù)組變量中訪問元素,而是在實際數(shù)組變量上調(diào)用 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 屬性為我們提供了數(shù)組索引 0 處的元素。 該數(shù)組沒有元素,因此 arr[0] 的計算結(jié)果為 undefined 并對其調(diào)用 push() 會導致錯誤。

為了解決這個問題,我們需要在數(shù)組變量上調(diào)用 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 的變量調(diào)用 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);

這里的問題是我們曾經(jīng)明確地將變量設置為未定義,但后來我們在變量上調(diào)用了 push()。 對此的修復將根據(jù)您的情況而有所不同。

也許您在再次使用之前忘記將變量設置為定義的值:

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 的對象屬性調(diào)用 push() 方法

要修復 JavaScript 中的“無法讀取未定義的屬性‘push’”錯誤,請確保調(diào)用 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);

在這種情況下,出現(xiàn)錯誤是因為索引 1 處的對象元素沒有 score 屬性。

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

從對象訪問不存在的屬性不會在 JavaScript 中引發(fā)錯誤,而是會為您提供 undefined 值。 如果您嘗試在該不存在的屬性上調(diào)用類似 push() 的方法,您將遇到錯誤。

在這種情況下,我們可以通過將第二個數(shù)組元素的 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);
責任編輯:華軒 來源: 今日頭條
相關推薦

2014-03-17 09:22:43

Linux命令

2009-11-23 15:57:51

PHP偽靜態(tài)

2021-03-10 10:13:39

爬蟲Python代碼

2011-06-22 15:21:08

XML

2020-08-10 00:30:55

備份密碼iPhone移動安全

2009-02-25 09:52:14

類型轉(zhuǎn)換.NET 強制轉(zhuǎn)型

2009-03-31 13:12:30

解析XMLJava

2022-11-04 13:35:29

IT遠程工作混合工作

2022-12-07 10:28:22

2016-06-28 10:19:31

云計算云安全

2010-07-16 13:50:53

Perl哈希表

2009-09-17 16:55:58

C#組件設計

2020-01-21 19:15:23

漏洞安全IT

2010-03-18 17:57:37

Java XMLSoc

2010-08-02 16:47:46

Flex

2020-07-24 09:56:12

React開發(fā)數(shù)據(jù)

2014-02-28 10:50:24

Linux命令

2021-09-03 11:24:04

云計算云計算環(huán)境云應用

2025-03-12 09:54:02

2023-02-03 08:47:20

職位招聘難題
點贊
收藏

51CTO技術棧公眾號