gpt4 book ai didi

javascript - Node.js v8 HOLEY 数组意外行为

转载 作者:行者123 更新时间:2023-11-30 20:16:26 26 4
gpt4 key购买 nike

看完Mathias Bynens关于 HOLEY 和 PACKED 阵列的报告,我做了一些实验并得到了意想不到的行为。看两种情况:

// CASE #1
const array = [0,1,2,3,4,5,undefined]
array.__proto__[6] = 'some value'

console.log(array[6]) // "some value" expected but got undefined

// CASE #2
const array = [0,1,2,3,4,5]
array[10] = 'faraway value'
array.__proto__[6] = 'some value'

console.log(array[6]) // "some value" expected and received

那么,这些情况之间有什么区别?为什么在第一种情况下它不查看原型(prototype)链就返回 undefined

最佳答案

在案例 #1 中,您明确地将 undefined 放入数组中。没有必要沿着原型(prototype)链往上走,因为元素已经存在。比较:

var case1 = [0, 1, 2, 3, 4, 5, undefined, 7];
var case2 = [0, 1, 2, 3, 4, 5, , 7];
case1.hasOwnProperty(6); // true
case2.hasOwnProperty(6); // false
console.log(case1[6]); // "undefined", loaded from object
console.log(case2[6]); // "undefined", because no element was found (not on the object, and not on the prototype chain)
case2.__proto__[6] = "proto value";
console.log(case2[6]); // "proto_value", loaded from prototype chain

关于javascript - Node.js v8 HOLEY 数组意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51879072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com