cuốn sách gpt4 ai đã làm

javascript - JavaScript 中 [undefined × 2] 和 [undefined, undefined] 之间的区别

In lại Tác giả: Walker 123 更新时间:2023-11-28 13:10:57 32 4
mua khóa gpt4 Nike

từhttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of ,

Note: this implies an array of 7 empty slots, not slots with actual undefined values

这是什么意思?

const foo = Array(2)
không xác định
const bar = [undefined, undefined]
không xác định
foo[0] === bar[0]
ĐÚNG VẬY
foo
[undefined × 2]
bar
[undefined, undefined]

他们有什么区别?是否存在我只能使用其中一个而不能使用另一个的用例?

câu trả lời hay nhất

foo 有两个未填充任何内容的空槽(在控制台中解析为 không xác định). bar 填充了 không xác định giá trị.

因此,例如 Array.prototype.map 被定义为仅在设置值时才起作用:

foo.map(() => console.log('test'))
// nothing

bar.map(() => console.log('test'))
// test
// test

关于javascript - JavaScript 中 [undefined × 2] 和 [undefined, undefined] 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42474796/

32 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress