Tutkitaan kuinka JavaScriptissä luodaan olio (object). JavaScriptissä voidaan luoda olio ilman, että ensin tarvitsee luoda luokka.
- Luo tiedosto nimeltään book.js
- Luo tiedostoon object nimeltään book
- Kopioi arrayExample:n bookArray book objektin sisään
- Lisää objektiin metodit
- getAllBooks(), joka tulostaa koko bookArrayn konsolille (huom! this-osoitin)
- getOneBook(x), joka tulostaa sille argumenttina(x:n arvo) annetun kirjan tiedot
- addBook(id_book, name, author, isbn), joka lisää sille annetun kirjan bookArrayhin (käytä push metodia)
- Testaa jokaista metodia
Let's explore how to create an object in JavaScript. In JavaScript, you can create an object without first defining a class.
- Create a file named book.js.
- In the file, create an object named book.
- Copy the `bookArray` from arrayExample into the `book` object.
- Add the following methods to the object:
- getAllBooks() – Prints the entire `bookArray` to the console (note: use the `this` keyword).
- getOneBook(x) – Prints the details of the book specified by the argument `x`.
- addBook(id_book, name, author, isbn) – Adds the given book to `bookArray` (use the `push` method).
- Test each method.