For learning purposes I've created a JS-File. I implemented a Method inside the JS-Object to get a BookStorage counter. The thing is everytime I add Books the Browser gives me back the default value for the Object. Anyone have an Idea?
Here is some snippets of my Code:
Book-Class:
class Book{
constructor(
name,
quantity
){
this.name = name,
this.quantity = quantity
}
showStorage(param) {
if(param != null){
return this.quantity + param;
this.quantity += param;
}
else {return this.quantity;}
}
}
Main-Script:
import Book from "./Book.js"
const lordOfRings = new Book(
"Lord of the Rings",
10
)
console.log("The Book:", lordOfRings.name);
console.log("The number in Storage:", lordOfRings.showStorage());
console.log("Adding 10 Books to storage:", lordOfRings.showStorage(10));
console.log("Adding 10 Books to storage:", lordOfRings.showStorage());
console.log("New Storage amount of Books:", lordOfRings.showStorage(10));
My expected output was:
"Lord of the Rings"
10
20
20
30
Instead of that I get:
"Lord of the Rings"
10
20
10
20
paramisundefined, notnull.showStoragemethod, youreturnbefore changing the value ofthis.quantity. Yourreturnstatement should be the very last thing you do in your method.