我有一个关于数组中的空和未定义的问题
请参阅我的代码底部
const arr = []
arr[1]=1
arr[2]=2
arr[3]=3
arr[5]=5
console.log(arr[4])// console: undefined
console.log(arr)// console: [empty, 1,2,3,empty,5]
Run Code Online (Sandbox Code Playgroud)
所以我不明白两个控制台结果之间的区别
为什么 console.log(arr[4]) 未定义但 console.log(arr) 的索引 4 为空?
请帮助我,谢谢
在 React 中,我知道下一个代码在 mount 中调用 foo 一次
(...)
function foo() {
return {
id: 1,
text: 'hello',
}
}
const App = () => {
const [value, setter] = useState(foo);
(...)
Run Code Online (Sandbox Code Playgroud)
但是如果我将 foo(useState 的参数) 更改为 foo() 如果应用程序正在重新渲染,则此代码将调用 foo 。
我知道 foo 和 foo() 之间的区别,但我知道为什么会出现这种差异