简体   繁体   中英

Assign an object to property using Object Literal Syntax

I need to pass an object as property to other object using Object Literal Syntax, something like this:

var obj = functionReturnObject();
callOtherFunction({propertyName: obj});

I know there are other simple ways to do this, eg

var obj = functionReturnObject();
var auxObj= {};
auxObj.propertyName = obj ;
callOtherFunction(auxObj);

or

callOtherFunction({propertyName: functionReturnObject()});
//This way is not useful for me because I need to keep a copy of the object before use it as parameters to the function callOtherFunction()

I am wondering if there is any straight way to do this in literal syntax to avoid using an auxiliar object like auxObj

You don't need a auxObj , just call like your sample:

callOtherFunction({
   propertyName: functionReturnObject(), 
   propertyAge: 20, 
   sex: 'M', 
   test: function() {
      // some process
      return 0;
   }   
});

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