2013-04-28 57 views
0

這一定是我沒有把握但一些JavaScript概念..不確定的東西是明確

對於一些奇怪的原因,ID的 thisSound返回undefined!爲什麼??

console.log("o: "+o.id+" - "+decodeURI(soundURL)); 
// create sound 
thisSound = soundManager.createSound({ 
    id:o.id, 
    url:decodeURI(soundURL) 
}); 

console.log("thisSound: "+thisSound.id+" - "+thisSound.url); 

控制檯:

o: Sound0 - http://api.soundcloud.com/tracks/76673175/stream?client_id=e992d357c0914e9b65ba17f459720fc0 

thisSound: undefined - http://api.soundcloud.com/tracks/76673175/stream?client_id=e992d357c0914e9c65ba17f459720fc0 

回答

1

您提供的代碼不會「明顯」定義返回對象的id

// create sound 
thisSound = soundManager.createSound({ 
    id:o.id, 
    url:decodeURI(soundURL) 
}); 

比方說,我寫的功能createSound爲您提供:

var soundManager = { 
    createSound: function (options) { 
    // do internal magic here 
    createSound(options); 
    // return public API here 
    return { 
     getId: function() {} 
    } 
    } 
}; 

所以,在這裏我的觀點是,如果有一個第三方的功能,您應該遵循誰創建功能的文檔,SoundManager顯然不會返回一個定義有id屬性的對象。它「returns a SMSound object instance」 - 這個對象是什麼,請在文檔中找到。

0

我一無所知的SoundCloud庫,雖然功能createSound似乎asyncronous ......(這就像I/O的東西,最的I/O操作在JavaScript和其他語言 - 是異步)..是否有可能當你調用console.log thisSound尚未完全創建?也許你可以用很多時間模擬一個setInterval()的行爲,並且這段代碼可以工作......(儘管我相信你應該爲此使用回調函數)。