javascript - Why Babel compile 'this' keyword as undefined inside a prototype function? -
this question has answer here:
- when should use arrow functions in ecmascript 6? 6 answers
- babel replaces undefined 2 answers
i started learn es6
used babel
compile code, when assign this
keyword variable inside prototype method compiled undefined
is bug? or problem 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']);
see this: https://developer.mozilla.org/en-us/docs/web/javascript/reference/functions/arrow_functions
shortly, in arrow function, this
has no binding.
Comments
Post a Comment