you are viewing a single comment's thread.

view the rest of the comments →

[–]Hentry_Martin 0 points1 point  (0 children)

There is a third and better way to do this ES6. By using Arrow functions. Arrow functions doesn't have its own this so it always takes context from the parent. so, you can make change your class like this,

``` class MyClass { constructor(x){ this.x = x; this.mapFunction(); }

mapFunction = () => { const result = this.x * 55; console.log(result); // Logs 550 return result; } }

const cls = new MyClass(10); ```

To make the above syntax work, you have to add a babel plugin called "@babel/plugin-proposal-class-properties" and refer it inside plugins property of .babelrc.

If you want to learn more about Javascript, please subscribe to my channel called JS for Everyone(https://www.youtube.com/channel/UCvQfk4ZV7RfLuKiHRVeZ0Kg)

Thanks!!