简体   繁体   English

此代码段中的加法器是什么? 我找不到答案

[英]What is adder in this code snippet? I can't find an answer

I'm trying to figure out what is a const variable if we assign it to a array.push method 我试图找出什么是const变量,如果我们将其分配给array.push方法

const numbers = [0,33,4,1]
const adder = numbers.push(2)

What does adder become here ? 加法器在这里变成什么? If i write it in the console it gives me the length of the numbers array. 如果我在控制台中编写它,它将为我提供数字数组的长度。 Is it bad practice to assign an array method to a variable ? 将数组方法分配给变量是不好的做法吗?

What does adder become here ? 加法器在这里变成什么?

It's assigned the return value of push . 分配了push的返回值。 So look at the documentation for push : 因此,请查看push的文档

The push() method adds one or more elements to the end of an array and returns the new length of the array. push()方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。

So it is the length of the array. 所以它是数组的长度。


Is it bad practice to assign an array method to a variable ? 将数组方法分配给变量是不好的做法吗?

In general (there are exceptions) it isn't useful to assign an array method to a variable … but that isn't what this code is doing. 通常 (有例外),将数组方法分配给变量并没有什么用处 ……但这不是此代码的作用。 The return value of a method is not the method itself. 方法的返回值不是方法本身。

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

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