2013-04-28 46 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()的行为,并且这段代码可以工作......(尽管我相信你应该为此使用回调函数)。

相关问题