I have an object that takes arguments, I would like to distribute the objects, so each property is an argument in this function.
What am I doing wrong in my code?
const args = { a: 1 b: 2 } const fn = (a, b) => a + b // i am trying with no success console.log(fn(...args))
You can use ES6 object destructuringfor the passed parameter, and then just pass in your object.
object destructuring
const args = {a: 1, b: 2} const fn = ({a, b}) => a + b console.log(fn(args))
You can also set default values ββfor these properties.
const args = {b: 2} const fn = ({a = 0, b = 0}) => a + b console.log(fn(args))
, , . . Object.values (ES 2017), .
Object.values
const args = { a: 1, b: 2 } const fn = (a, b) => a + b fn(...Object.values(args));
, , Object.values . , a b, Object.keys(args) .
a
b
Object.keys(args)
:
const fn = ({a, b}) => a + b
, , , . . , , . , , .
const args = { a: 1, b: 2, argumentify: function () { return [this.a, this.b]; } }; const fn = (a, b) => a + b; console.log(fn(...args.argumentify()));
1) , .
2) ( ( ) ).
3) .
Source: https://habr.com/ru/post/1691464/More articles:std :: function is heavier than automatically saving lambda functions - lambdaThread.Sleep vs. Task.Delay when using timeBeginPeriod / Task scheduling - multithreadinghttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1691461/attributeerror-pandasexprvisitor-object-has-no-attribute-visitellipsis-using-pandas-eval&usg=ALkJrhgkXDOi5nMiBzmHqDN8gyB2G0ZuygCannot start debugging on the web server. Visual Studio WITHOUT admin - c #Debugging a website on local IIS without administrative privileges - c #Symfony Router redirects to http in https - httpR - combined fuzzy and exact match - matchingWhat does class deadlock mean? - javaASP.NET Core API sending double quoted string - c #Replace the item and the following items in the list if the condition is met - pythonAll Articles