-1

I am learning vue and I came across this weird syntax in defining a function, I am curious what it is called and where i can learn more about it

register({commit}, credentials){
}

Why are there brackets around commit? Shouldn't it just be simple:

register(commit, credentials){
}

What's the purpose of placing brackets around commit variable?

az2902
  • 418
  • 6
  • 18

1 Answers1

1

This is object destructuring assignment. You can read about it on MDN here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Basically, your function's first parameter is an object that has a commit property. Destructuring lets you pull that property out of the object. Here's an example of destructuring being used (but not within function arguments):

const obj = {
  commit: "Hi there"
}

const { commit } = obj;
console.log(commit);
// "Hi there"
Nick
  • 16,066
  • 3
  • 16
  • 32