Pimptech Pimptech - 4 months ago 18
Javascript Question

ES6 + BabelJS + Webpack to export classes

I was learning about babel and the new stuff ES6 that looks amazing, but I'm struggling with import classes from different 'js'.

I have the dummy.js:

export default class Test {
construct(){
this.name = "name";
}

getName(){
return this.name;
}
}


And i have my index.js (entry point on webpack config):

import Test from './dummy.js';
var ab = new Test();

console.log(ab);
//out: Test{}
console.log(ab.getName());
//out: undefined


I spent some hours googling but I've not been able to find anything that works. I tried to use 'module.exports' or even export a function to return new instance of 'Test', but it doesn't work too.

The only way that works was utilizing prototype, it seems to do the work, but before coding everything utilizing prototype i want to be sure that class doesn't work.

I even tried make 'dummy.js' an entry of webpack config, but it would implicit a static class and i don't want it.

Then anyone know how to bypass this ? Or I'm forgetting something ?

Answer

The name of constructor in ES2015+ is constructor not construct.

References:

Comments