Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does object exported in ES6 module have undefined methods?

I have an ES6 class defined in an ES6 module that exports an instance of that class:

class MyObject {
  constructor() {
    this.propertyA = 1;
    this.propertyB = 2;
  }

  myMethod() {
    doStuff();
  }
}

var theInstance = new MyObject();
export default theInstance;

When I import this module, myMethod is undefined:

import * as theObject from './my/module';

theObject.myMethod(); // Error! undefined is not a method.

The properties defined in the constructor do exist. It's as though the object's prototype was excluded, and only its members were exported.

I am requiring 'babel/register'.

Why is exporting this object not working correctly?

like image 601
Jacob Avatar asked Jul 23 '15 23:07

Jacob


1 Answers

I figured this out right after asking. It looks like there's a difference between import * as foo from 'module' and import foo from 'module'. This works:

import theObject from './mymodule';

So it wasn't a matter of the wrong thing being exported, but it was being imported incorrectly.

like image 84
Jacob Avatar answered Sep 26 '22 00:09

Jacob