javascript - Why Babel compile 'this' keyword as undefined inside a prototype function? -


this question has answer here:

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']); 

code snippet screenshot

see this: https://developer.mozilla.org/en-us/docs/web/javascript/reference/functions/arrow_functions

shortly, in arrow function, this has no binding.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -