简体   繁体   English

使用对象文字语法将对象分配给属性

[英]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 我想知道是否有任何直接方法可以使用文字语法来避免使用像auxObj这样的辅助对象

You don't need a auxObj , just call like your sample: 您不需要auxObj ,只需像样例一样调用即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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