我正在尝试JavaScript对象,而我只是坚持为什么我的代码中出现TypeError。
var marvelFunFact = {
captainMarvel: [
{
info: "Did you know that info 1",
imageUrl: "url.com/image1.jpg"
},
{
info: "Did you know that info 2",
imageUrl: "url.com/image2.jpg"
},
{
info: "Did you know that info 3",
imageUrl: "url.com/image3.jpg"
}
],
lastName: "Doe",
};
var marvelFunFactContext = "captainMarvel";
var rand = marvelFunFact.marvelFunFactContext[Math.floor(Math.random() * marvelFunFact.length)].info;
出现的错误是“ TypeError:marvelFunFact.marvelFunFactContext未定义”。我只是一个初学者,不要判断,这可能只是一个简单的解决方法:/