Search code examples
javascriptjavascript-objects

Pass parameters as an object Javascript


In python there is a convenient way of passing a dict to a function instead of variables, like here

user = {
    'firstname':'John',
    'lastname': 'Wick'
}

def fullname(firstname, lastname):
    return firstname + ' ' + lastname

fullname(**user)
>>> 'John Wick'

instead of

fullname(user['firstname'], user['lastname'])
>>> 'John Wick'

Q: Is there a similar way of passing an object to a function instead of variables in javascript?

upd: function is unchangeable


I tried using the ... on the object, but it causes an error

user = {
    'firstname':'John',
    'lastname': 'Wick'
}

function fullname(firstname, lastname){
    return firstname + ' ' + lastname;
}

fullname(...user)
>>> Uncaught TypeError: Found non-callable @@iterator

Solution

  • I'm not at the pc, so this could be wrong by try this:

    fullname(...Object.values(user))