My following code is working, but when I use ES6 syntax it is not working anymore. Please explain what is happening?
This code is fully working.
function Library(){ this.books = [];};
Library.prototype.addBook = function(book){
this.books.push(book);
};
Library.prototype.getBook = function(index){
return this.books[index];
};
var m = new Library();
m.addBook('The Demon Haunted World');
m.getBook(0);
// output will be like 'The Demon Haunted World'
Then I changed the syntax to some extent of ES6. The code then looked like this:
function Library(){ this.books = [];};
Library.prototype.addBook = (book) => {
this.books.push(book);
};
Library.prototype.getBook = (index) => {
return this.books[index];
};
var m = new Library();
m.addBook('The Demon Haunted World');
Why is this not working? I'm getting the following error:
VM505:2 Uncaught TypeError: Cannot read property 'push' of undefined(…)