gpt4 book ai didi

javascript - 为什么 x.push[0] 没有产生错误?

转载 作者:行者123 更新时间:2023-11-29 17:50:46 25 4
gpt4 key购买 nike

我打错了,使用 x.push[0] 而不是 x.push(0)。但它没有产生错误并返回 undefined 这让我很困惑。

test

最佳答案

b.push 是一个函数,但函数只是另一种值——[] 运算符可以尝试访问属性在任何一种值(value)上。但是,如果未找到该属性,结果将是 undefined,这里就是这种情况;函数通常没有 0 属性。

var b = [];
var someFunction = b.push;

console.log('0' in someFunction); // false; function doesn’t have a 0 property
console.log(someFunction[0]); // undefined for the same reason

console.log('length' in someFunction); // true; functions have a length property!
console.log(someFunction['length']); // 1; just like someFunction.length

关于javascript - 为什么 x.push[0] 没有产生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44014197/

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