REST API with Express.js
Object Example
Tutkitaan kuinka JavaScriptissä luodaan olio (object). JavaScriptissä voidaan luoda olio ilman, että ensin tarvitsee luoda luokka.
  1. Luo tiedosto nimeltään book.js
  2. Luo tiedostoon object nimeltään book
  3. Kopioi arrayExample:n bookArray book objektin sisään
  4. 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)
  5. 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.
  1. Create a file named book.js.
  2. In the file, create an object named book.
  3. Copy the `bookArray` from arrayExample into the `book` object.
  4. 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).
  5. Test each method.



Toggle Menu