Class identifiers refer to the class constructor function.
Replace
let libro1 = [titulo1, autor1, a?o1, genero1];
libro1.push(new Libro(titulo, autor, a?o, genero))
console.log(libro1.Libro());
with
let libro1 = new Libro(titulo, autor, a?o, genero)
libro1.informacion() // call the informacion method of libro1
which creates a class instance object, assigns it to libro1
, and then calls the informacion
method of libro1
.
If you want an array of book objects, you could save libro1
(and other books) in an array:
const libri = [];
... // create libro1
libri.push(libro1);
... // create libro2
libri.push( libro2);
To create more than one book, try putting the prompts inside a function that returns a Libro
class object. That way all books use the same set of prompts.
In response to comment, the informacion
method already calls console.log
and returns undefined
if that is what console.log
returns. Returning an information string may better suit your purpose:
informacion() {
return `El libro ${this.titulo} del autor ${this.Autor} fue publicado en el a?o ${this.A?o} y es de genero ${this.Genero}`;
}
If the result of the prompts is saved in an array, like
let arr = [titulo, Autor, A?o, Genaro];
you can pass them to the constuctor function using rest parameter syntax
:
let libro = new Libro( ...arr );
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…