Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass function arguments to Object.assign

Tags:

javascript

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. So the function would resemble the following:

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

Thanks in advance :)

like image 877
Le Moi Avatar asked Apr 16 '16 10:04

Le Moi


People also ask

How do you pass an argument to an object?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

Can I pass a function in an object?

Methods—setting functions as properties of objects. In JavaScript, you can use functions as values, just like numbers, strings, and objects. That means you can pass them as arguments, return them from other functions, and set them as properties of objects.

Can I pass object in function JavaScript?

We can pass an object to a JavaScript function, but the arguments must have the same names as the Object property names.

How can you get the type of arguments passed to a function?

There are two ways to pass arguments to a function: by reference or by value. Modifying an argument that's passed by reference is reflected globally, but modifying an argument that's passed by value is reflected only inside the function.


1 Answers

If you're using ES2015 rather than just a shim, you can use spread notation, Array.from, and slice:

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

Or just using slice directly rather than after 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.

If you're not using ES2015, you can do the same thing with just a shimmed Object.assign via apply:

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 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.

So say you have:

obj.foo(1, 2, 3);

The equivalent using Function#apply is:

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

The first argument, obj, tells apply what to use as this during the call. 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.

like image 185
T.J. Crowder Avatar answered Sep 30 '22 12:09

T.J. Crowder