简体   繁体   English

Javascript/React 中 function 末尾的 [] 是什么意思?

[英]What does the [] mean in the end of the function, in Javascript/React?

Could someone give me the direction where to look, or let me know what does the square brackets mean at the end of the function.有人可以告诉我在哪里看,或者让我知道 function 末尾的方括号是什么意思。 Is there a different way how this can be re-written?有没有不同的方法可以重写它?

Code Example:代码示例:

  useEffect(() => {
if (user) {
  router.push('/profile');
}
 }, [user]);

So the part which I am trying to understand is the [user].所以我试图理解的部分是[用户]。

Can this be re-written differently?这可以用不同的方式重写吗? What does it mean from the code perspective?从代码的角度来看,这意味着什么?

This is called an Array literal .这称为Array文字

An Array is an object that represents the concept of an indexable sequence of values . Array是一个 object ,它表示值的可索引序列的概念。 A literal is a syntactic element of the programming language that allows one to "literally" write down an object value, as opposed to programmatically constructing it.字面量是编程语言的一种句法元素,它允许人们“字面上”写下object 值,而不是以编程方式构造它。

Here's an example of the difference between programmatically constructing an array and directly writing down the same array:这是以编程方式构造数组和直接写下相同数组之间的区别的示例:

const ary1 = Array.from({ length: 10 }, (_, i) => i);
const ary2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM