gpt4 book ai didi

javascript - 在函数参数中解构后返回相同的对象

转载 作者:行者123 更新时间:2023-12-03 03:01:58 25 4
gpt4 key购买 nike

假设我有一个对象

var bar = {hi: 1, there: 2};

我想在函数末尾返回与传递的同一对象,同时在函数参数中进行解构赋值。

它可能看起来像这样:

function foo({hi, there}){
//logic with variables "hi" and "there"
return ...arguments;
}

并且返回值与 bar; 相同由于显而易见的原因,扩展运算符在这种情况下不起作用,但我想知道是否有一种简单的方法可以做到这一点或类似的事情。

最佳答案

分别命名和解构:

function foo(obj) {
let {hi, there} = obj;
// logic with variables "hi" and "there"
return obj;
}

关于javascript - 在函数参数中解构后返回相同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341241/

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