Pimptech Pimptech - 1 year ago 264
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 {
this.name = "name";

return this.name;

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

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

//out: Test{}
//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 Source

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download