在javascript中使用新对象内的返回值

时间:2012-03-09 18:10:01

标签: javascript

这可能太简单了,但对象语法正在向我发展。

我有一个简单的函数,它返回一个非常适合在对象上用作参数的字符串,所以:

function createString() {

// yadda yadda yadda

    return myPerfectstring;
}

然后就是这个对象,它应该将myPerfectstring作为这样的值:

new myPlayer({
        this: "#that",
        css: "#theOther"
    },
        //  insert myPerfectstring here
        // if I log it to the console and copy-paste here, everything works wonders
    , {
        other: "nana",
        things: "yeah",
        around: "yeahyeah"
    });

我知道我不能只在那里抛出函数,也不能将它存储在变量中并插入,所以,我如何输入该字符串,好像它实际上是对象的一部分?

2 个答案:

答案 0 :(得分:2)

只需将其内联:

new myPlayer({
        this: "#that",
        css: "#theOther",
        theString: createString(),
        other: "nana",
        things: "yeah",
        around: "yeahyeah"
    });

答案 1 :(得分:2)

你不能这样做吗?我无法确定您是如何尝试在构造函数中使用返回值。作为自己的财产?

new myPlayer({
  this: "#that",
  css: "#theOther",
  string: createString(),
  other: "nana",
  things: "yeah",
  around: "yeahyeah"
});