简体   繁体   中英

javascript function arguments to object

here is using function to insert object to mongo

function addTransaction(userId,paymentMethod,product={},status){
  Transactions.insert({
    userId:userId,
    paymentMethod:paymentMethod,
    product:product,
    status:status
  });
}

is there any better way to generate the object from arguments or pass the whole object arguments. If so, how to make an object more efficient than like this

var dataObj = {
  userId:userId,
  paymentMethod:paymentMethod,
  product:product,
  status:status
}

may be can I create an object by var name as key and value

You can use short object notation :

Transactions.insert({
  userId, // equivalent to userId: userId
  paymentMethod,
  product,
  status
});

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