gpt4 book ai didi

javascript - let arrayName = [...this.A, this.B] 和 let arrayName = [...this.A] arrayName.push(this.B) 之间的区别

转载 作者:行者123 更新时间:2023-12-01 00:07:27 24 4
gpt4 key购买 nike

所以我一直在观看 freeCodeCamp 的 Vue Youtube 教程。

他们曾经使用过这段代码:

let answers = [...this.currentQuestion.incorrect_answers];
answers.push(this.currentQuestion.correct_answer);

然后是这个:

let answers = [...this.currentQuestion.incorrect_answers, this.currentQuestion.correct_answer]

它们有什么不同吗?我更熟悉推送方法,但如果其他方法有效并且没有任何缺点,为什么还要推送呢?

最佳答案

它们基本上是等价的,它们都将 JS 迭代器解压到数组中,并向该数组追加一个新值。第一个方法通过改变数组来实现,第二个方法在构造时实现。

关于javascript - let arrayName = [...this.A, this.B] 和 let arrayName = [...this.A] arrayName.push(this.B) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60281088/

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