This code:
'use strict'
function Ob() {}
Ob.prototype.add = () => {
this.inc()
}
Ob.prototype.inc = () => {
console.log(' Inc called ');
}
module.exports = new Ob();
Is used by this code:
'use strict'
const ob = require('./ob')
ob.add();
When calling the ladder one, I get this error:
this.inc is not a function
When I change the first snippet to this:
'use strict'
function Ob() {}
Ob.prototype.add = function() {
this.inc();
}
Ob.prototype.inc = function() {
console.log(' Inc called ');
}
module.exports = new Ob();
Everything is fine and I get:
Inc called
Why does the first version throw?
Update: How would I make it work using arrow functions?