2012-03-09 33 views
0

這可能太簡單了,但對象語法正在接近我。在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" 
    }); 

我知道我不能只是拋出函數那裏,既不存儲它在一個變量和插入,所以,我怎麼去輸入該字符串,就好像它實際上是對象的一部分?

+0

你必須更具體一些,提供一個更好的例子。 'newPlayer'是一個函數,似乎你想傳遞三個參數給它。我不明白「對象」部分。你想'新的myPlayer({...},createString(),{...})'? **是什麼**'myPerfectstring'? – 2012-03-09 18:12:34

+0

按照約瑟夫西爾伯和科學家的說法,或者看看這個小提琴:http://jsfiddle.net/MQND7/ – jack 2012-03-09 18:16:16

回答

2

你不能這樣做呢?我無法確定如何在構造函數中使用返回值。因爲它是自己的財產?

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

這樣做。其實這是我從一開始就想做的,但我不知道對象只會接受一個數組,而不是一個字符串,這就是爲什麼它不工作,我認爲使用該功能也不是。 – moraleida 2012-03-12 19:40:31

2

只是把它內聯:

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