Before getting started with our next sections, the readers may have noticed that we have not used a fat arrow function as a replacement for the callbacks in all cases. This is because we will be using the context of a function (this) widely. There is a difference in implementation between flat arrow functions and regular functions when it comes to context. The differential statement is that the context of function (this) doesn't depend on where it is declared, but from where it is called. To understand this, let's consider the following example:
function getData() {
console.log(this.a); // global
}
getData.a = 'hello';
var a = 'world';
getData();
On running the preceding snippet, we will get the output 'world'; this is because the foo function is called in a global scope, hence the context is global and we receive the value of the global variable. To restrict this behavior, we can use the bind method or es6 arrow functions.
Now, we convert the preceding get virtual method as follows:
Account.virtual('fullname')
.get(()=>{
return this.firstname + ' ' + this.lastname;
})