简体   繁体   English

将函数参数传递给Object.assign

[英]Pass function arguments to Object.assign

Say I have a function to compose an object from other objects, and I am passing arguments to the function - initially an object literal, and then the objects I want to compose to extent the object: 假设我有一个函数从其他对象组成一个对象,我将参数传递给函数 - 最初是一个对象文字,然后是我想要组合的对象来扩展对象:

composeFunc({}, obj1, obj2, obj3);

The number of args passed is optional, how do I then pass the args to Object.assign() starting at the 2nd arg. 传递的args数是可选的,我如何从第二个arg开始将args传递给Object.assign() So the function would resemble the following: 所以该函数类似于以下内容:

function composeObj(objs) {
    return Object.assign(arguments[1], arguments[2], arguments[3]... etc);
}

Thanks in advance :) 提前致谢 :)

If you're using ES2015 rather than just a shim , you can use spread notation, Array.from , and slice : 如果您使用的是ES2015而不仅仅是垫片,则可以使用扩展符号, Array.fromslice

 function composeObj(objs) { return Object.assign(...Array.from(arguments).slice(1)); } 

Or just using slice directly rather than after Array.from : 或者直接使用slice而不是使用Array.from

 function composeObj(objs) { return Object.assign(...Array.prototype.slice.call(arguments, 1)); } 

...see Thomas' answer using rest args, as that's the right way to do this in ES2015. ...使用rest args查看Thomas的回答 ,因为这是在ES2015中执行此操作的正确方法。

If you're not using ES2015, you can do the same thing with just a shimmed Object.assign via apply : 如果你没有使用ES2015,你可以通过apply仅使用填充的Object.assign做同样的事情:

 function composeObj(objs) { return Object.assign.apply(Object, Array.prototype.slice.call(arguments, 1)); } 

There, we're using Function#apply instead of the spread operator (since ES5 and earlier don't have the spread operator). 在那里,我们使用Function#apply而不是spread运算符(因为ES5和更早版本没有spread运算符)。 Function#apply calls the function you call it on using the first argument as this during the call, and using the array (or array-like thing) you give it as a second argument as the arguments for the call. Function#apply调用你怎么称呼它使用的第一个参数的函数this在通话过程中,并使用阵列(或阵列状的东西),你给它的第二个参数作为参数呼叫。

So say you have: 所以说你有:

obj.foo.apply(obj, [1, 2, 3]);

The equivalent using Function#apply is: 使用Function#apply的等价物是:

 obj.foo.apply(obj, [1, 2, 3]); 

The first argument, obj , tells apply what to use as this during the call. 第一个参数, obj ,告诉apply如使用什么this在通话过程中。 The second argument is an array of arguments to use. 第二个参数是要使用的参数数组。

But if you use the spread operator, there's no need, it spreads out its array-like operand into discrete arguments. 但是如果你使用扩展运算符,则没有必要,它将类似数组的操作数扩展为离散参数。

ES2015: ES2015:

function composeObj(objs, ...rest){
  return Object.assign(...rest);
}

rest will be a true array of all of the arguments starting from the second one. rest将是从第二个开始的所有参数的真实数组。

And the Babel-output: 和巴别塔输出:

function composeObj(objs) {
  for (var _len = arguments.length, rest = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    rest[_key - 1] = arguments[_key];
  }

  return Object.assign.apply(Object, rest);
}

This seems to be a utility-function that might be used all over the place. 这似乎是一个可以在整个地方使用的实用功能。 As soon as you pass arguments to an other function like Array.prototype.slice() or Array.from() , composeObj won't be optimized by the JS-compiler anymore. 一旦将arguments传递给其他函数(如Array.prototype.slice()Array.from()composeObj将不再由JS编译器进行优化。 So, don't do that. 所以,不要这样做。

Everything else has already been said on TJ Crowders Answer and the comments; 关于TJ Crowders Answer和评论已经说了其他一切; just showing the better implementation. 只是展示更好的实施。

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

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