获取在参数中解构的对象 [英] Get object that was destructured in parameter
问题描述
假设我有一个函数,该函数将经过分解的对象作为箭头函数中的参数:
Suppose I have a function that takes a destructured object as a parameter in an arrow function:
const myFunc = ({a, b, c}) => {
};
是否存在某种语法或语法,也可以让我将整个对象作为单个值获得?由于箭头功能未绑定arguments
,因此无法使用该功能.
Is there anyway or syntax that would allow me to get that whole object as a single value as well? Since the arrow function doesn't bind arguments
, I can't use that.
是否可以命名,类似以下内容:
Is it possible to name it, something along the lines of:
const myFunc = (allArgs: {a, b, c}) => {
console.log(allArgs);
};
myFunc({a:1, b:2, c:3}); // Output: {a:0, b:1, c: 2}
很显然,这不是一个破坏交易的方法,并且有很多变通办法(要么不使用箭头功能,不分解,或者在需要时重新创建对象),但我想知道为了方便起见.
Obviously, this isn't a deal breaker and there are plenty of workarounds (either don't use an arrow function, don't destructure, or recreate the object when I need it), but I'd like to know for convenience sake.
推荐答案
您可以很容易地破坏单个参数.您失去了参数中简洁的解构语法的优势,但是它仍然很容易而且仍然很清楚:
You could destructure a single param quite easily. You lose the advantage of the terse destructuring syntax in the arguments, but it's easy and still clear:
const myFunc = (allArgs) => {
const {a, b, c} = allArgs;
console.log(allArgs);
};
或者,考虑是否需要箭头功能.如果您不需要访问词法this
,也许不错的老式function() {}
可以解决问题,并可以访问arguments
.
Alternatively, consider whether you need an arrow function. If you don't need access to the lexical this
perhaps a good old-fashioned function() {}
will do the trick and give you access to arguments
.
这篇关于获取在参数中解构的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!