简体   繁体   中英

What does it mean when a function takes an argument containing an equals = sign?

My function, defined inside a class, takes one object as an argument. There's an equals sign after the object, still inside the function argument definition. I haven't encountered this before, what does it mean? Does it default to passing an empty object if no email key/object pair is passed?

class UserAPI extends DataSource {

   async createUser({ email: emailArg } = {}) { 
      ...function goes here...          ^^^^
   }
}

That means default parameter . In your example if you don't provide any parameter or if you pass undefined , an empty object will be passed instead, and emailArg will be undefined as an empty object doesn't contain any email property:

 console.log({}.email);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM