I just started to learn ES6
so I used Babel
to compile that code, but when I assign this
keyword to a variable inside a prototype method it compiled to undefined
is this a bug? or problem with my code?
ES6 Code
function Prefixer(prefix) {
this.prefix = prefix;
}
Prefixer.prototype.prefixArray = arr => {
let self = this;
return arr.map((x) => {
console.log(self.prefix + x);
});
}
var pre = new Prefixer("Hello ");
pre.prefixArray(['Jeeva', 'Kumar']);
Babel Compiled Code
'use strict';
function Prefixer(prefix) {
this.prefix = prefix;
}
Prefixer.prototype.prefixArray = function (arr) {
var self = undefined;
return arr.map(function (x) {
console.log(self.prefix + x);
});
};
var pre = new Prefixer("Hello ");
pre.prefixArray(['Jeeva', 'Kumar']);