你好吗?现在,我正在用 Javascript 学习数组。我已经完成了一个将数组内容显示到 HTML 文件中的函数,但我只收到“未定义”。首先,我尝试修改“contenido”内部的代码,但我只将“字符”接收到0位置,例如:contenido += "<div id=d"+posicion+">Titulo: " +系列标题[0]。它给我返回了“D”,德克斯特。我究竟做错了什么?这是我的代码。/*Creating a class to structure the information of a TV show by saving the title, theme, array with the main actors and saving the favorite actor in the array.*/class SerieTV { constructor (titulo, tematica, actoresPrincipales){ var arrayActores = new Array(); this.titulo=titulo; this.tematica=tematica; this.actores=actoresPrincipales;/* Adding a function to generate a random favorite actor.*/ this.generaActorFavorito = function(){ var long = actoresPrincipales.length; let calc = Math.floor(Math.random()*(long)); arrayActores = actoresPrincipales[calc]; console.log(arrayActores); } }}/* Creating 3 series, the 1st with 2 actors, 2nd with 3 and 3rd with 4. Later, adding it to a new array called "total_series."*/var show01= new SerieTV('Dexter ', 'Drama ', ['Michael C Hall ' ,'Jennifer Carpenter']);show01.generaActorFavorito();var show02 = new SerieTV('Samurai Gourmet' , 'Cocina' , ['Naoto Takenaka' ,'Tetsuji Tamayama' , 'Honami Suzuki '] );show02.generaActorFavorito();var show03 = new SerieTV ('Breaking Bad ', 'Drama ', ['Aaron Paul ','Bryan Cranston ', 'RJ Mitte ', 'Anna Gunn ']); show03.generaActorFavorito();console.log("-------------------------");var total_series = new Array();total_series.push(show01);total_series.push(show02);total_series.push(show03);console.log(total_series);console.log("-------------------------");
2 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
要访问类的成员,请使用点表示法语法。
contenido += "<div id=d"+posicion+">Titulo: "+serie.titulo+"<br> Temática: "+serie.tematica+" <br> Actor Favorito: "+serie.actoresPrincipales+" <br> Actores: "+serie.actores+" <br><br>";
generaActorFavorito
另外,在这个语句的方法中,arrayActores = actoresPrincipales[calc]
您将一个数组重新分配为一个没有意义的值,您可以简单地执行var arrayActores;
而不是var arrayActores = new Array();
慕慕森
TA贡献1856条经验 获得超17个赞
好的,问题解决了。我解决了删除方括号的问题。谢谢你们!!
contenido += "<div id=d"+posicion+">Titulo: "+serie.titulo+"<br> Temática: "+serie.tematica+" <br> Actor Favorito: "+serie.actoresPrincipales+" <br> Actores: "+serie.actores+" <br><br>";
添加回答
举报
0/150
提交
取消