TypeError: _this2.function is not a function

518
September 06, 2017, at 7:39 PM

I have a small library with few classes. In one of the class, i am using method from other classes, thus I instantiate them, and use it in the code.

Some of the classes I use reference method with the this keyword.

example:

// index.js
import Class1 from './class1'
import Class2 from './class2'
import Class3 from './class3'
export {
   Class1,
   Class2,
   Class3
}
// class1.js
export default Class1 {
   func1() {
      return 'something';
   }
  func2() {
      return this.func1() + 'else';
  }
}
// class2.js
export default Class2 {
   func1() {
      return 'something';
   }
  func2() {
      return this.func1() + 'else again';
  }
}
// class3.js
import {Class1, Class2} from './index';
const c1 = new Class1();
const c2 = new Class2();
export default Class3 {
   funcNotWorking(){
       return c1.func2() + c2.func3() + 'this does not work'
   }
}

I am using this in tests by just importing it:

// test.js
const c3 = require('library.js').Class3
// test stuff calling:
c3.funcNotWorking()

I am compiling this with webpack and babel loader. This config seems to work as it compiles and everything else works, but this...

I have read that people seem to have trouble with this with React, but this is just Es6. Could someone point me to how to resolve this?

Answer 1

The context is of this is getting lost in my code.

I use recursion, mapping and arrow function in the code that was having problems. The Babel compiled version _this2 did not follow the context.

I extracted some methods, assigned them in the class constructor and use in the class the extracted method and it compiled correctly.

READ ALSO
Lighbox gallery not showing full screen images

Lighbox gallery not showing full screen images

im using http://lokeshdhakarcom/projects/lightbox2/ i need when i open an image to open on fullscreen not in smaller version, where can I changed? Thank you

180
Materialize css all tabs coming together below each other, but working properly after refresh

Materialize css all tabs coming together below each other, but working properly after refresh

I use materialize css tabs and it loads all the divs, one below the other on the first load

232
How to move columns in a table with horizontal and vertical scroll with the header fixed

How to move columns in a table with horizontal and vertical scroll with the header fixed

I am trying to display table which will have the both horizontal and vertical scrolling and the header will move horizontally but not vertically whereas the body will move both vertical and horizontalAnother requirement is the shift the position of the columns

172
Real time table AngularJS

Real time table AngularJS

I'm working with the Twitter stream APIMy server gets tweets from a concretes hashtags

215