In EcmaScript 5, we can alias this
as var ctrl = this
as shown in following snippet.
// EcmaScript 5
function BookController {
var ctrl = this;
ctrl.books = [];
ctrl.getBook = getBook;
function getBook(index) {
return ctrl.books[index];
}
}
Equivalent BookController
in ES6 using class
. I had a scenario in which getBook
is called with this
other than BookController
. In getBook
function, I want to make sure the context is always BookController
so I want to alias this
of BookController
in ES6.
// EcmaScript 6
class BookController {
constructor() {
this.books = [];
}
getBook(index) {
return this.books[index];
}
}
How to alias this
in JavaScript 2015 (EcmaScript 6)?