+ 1

What is the practical use of argument objects in JavaScript.

4th Aug 2020, 5:01 PM
Avinash
9 Respuestas
+ 5
[Response: Part 1 of 2] Someone Don't sell yourself short. You posted a solid answer here. 😉 Avinash As already mentioned, the arguments object contains all arguments passed into the function. That said, I wouldn't advise making it a practice to using the arguments object over named arguments. Named arguments give more context to the usage of input arguments making it clearer to developers using the function, as well as, developers supporting and maintaining the function long term. Consider the following example used earlier: function sum() { ... } It's not obvious that this function expects any input arguments much less unlimited arguments. On one hand, it could be referring to a global array ( puke... 🤮): var numList = [1, 2, 3, 4, 5, 6, 7, 8, 9] function sum() { return numList.reduce( (x, y) => x+y ) } Or... it could be using unnamed input args: (boo...) function sum() { return Array.from(arguments).reduce( (x, y) => x+y ) } This becomes more complex if we wanted to add another arg that wasn't meant to be included in the summation. Consider a silly example where the first arg should be true or false to indicate if the value should also be printed to console. The usage becomes even more ambiguous and the code becomes more complex. Consider this version: function sum() { let args = Array.from(arguments); let printSum = args.splice(0, 1)[0]; let result = args.reduce( (x, y) => x+y ) if(printSum === true) console.log(`result: ${result}`); return result; } Now... We really need to study the code to know how to use it.
5th Aug 2020, 10:10 PM
David Carroll
David Carroll - avatar
+ 4
[Response: Part 2 of 2] Avinash Ultimately, the preferred alternative for the above example would be: function sum(printSum, ...nums) { let result = nums.reduce( (x, y) => x+y ) if(printSum === true) console.log(`result: ${result}`); return result; } Or more simply: function sum(...nums) { return nums.reduce( (x, y) => x+y ); } NOTE: The arguments object isn't even available if using ES6 anonymous functions: const sum = () => Array.from(arguments).reduce( (x, y) => x+y ) Calling this function would produce an error indicating: "arguments is not defined" So... it's really best to let this legacy artifact be left in the museum and avoided moving forward.
5th Aug 2020, 10:15 PM
David Carroll
David Carroll - avatar
+ 3
Avinash it will be better to add 'arguments' tag in your question so others too can visit your question. Also mention other experts like David Carroll in your further questions so they too can see your questions and answer better.
5th Aug 2020, 7:16 AM
Roopesh
Roopesh - avatar
+ 2
https://code.sololearn.com/Wj6Hsmw06jD9/?ref=app This code may help you ..Look at it
5th Aug 2020, 10:33 PM
APU CHAND MALIK
APU CHAND MALIK - avatar
+ 2
Avinash one more thing you should know is : when you are creating some function like this: createUser(name, email, phone, address...) it definitely makes it really hard to work with it, because you may mistakenly type phone number in the place of email and email in the place of phone. In this case it will become very hard to debug it if you called it from depth. Instead of this there is a more pretty and easy way to do it. Look at this: function createUser (obj) { var name = obj.name, phone = obj.phone email = obj.email … } createUser({ name : "…”, phone : "…", email :"…" … }); This way is much more easy to maintain and handle and this method allows you to create optional variables. It is advised to use objects for functions with 4 or more argumets.
6th Aug 2020, 1:08 AM
Roopesh
Roopesh - avatar
+ 1
If you have a function which takes many arguments and if you don't know how many are they, arguments object is useful. arguments object contains all arguments inputed in your function. Look at this example that takes sum of give numbers: function sum () { var s = 0; for(var num of arguments){ s += num; } console.log(s) } sum(4,5,6,8,26,855,3) //907 arguments Object is like an array in which you can acces arguments in given order eg: if you have a function 'listNames' and user added arguments to this function as listNames("ada", "john", "haris") you can access each argument just like an array: function listNames() { console.log( arguments[0], //ada arguments[1], //john arguments[2], //haris ); //overall output :ada john haris } You can see how many inputs in your function has using arguments.length. ... continued Array.prototype.slice.call(arguments); //or var args = [].slice.call(
5th Aug 2020, 7:08 AM
Roopesh
Roopesh - avatar
+ 1
You can convert the arguments Object to Array. There are many ways to do it like: var args = Array.prototype.slice.call(arguments); //or var args = [].slice.call(arguments); //or var args = Array.from(arguments); //or var args = [...arguments]; By converting arguments to an array you can perform array operations with them You can get more information and useful examples in MDN : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
5th Aug 2020, 7:12 AM
Roopesh
Roopesh - avatar
+ 1
Thanks Someone . I will keep this in mind. You are a rockstar by helping coder
5th Aug 2020, 4:37 PM
Avinash
0
Someone help 🙏
4th Aug 2020, 5:01 PM
Avinash