0

I have a project structure looks like this.

src/core/sample1.js

class MyPro1 { 
  constructor() { 
  }

  print_data() { 
    return 'Prine';
  }

  smaple_data() { 
    console.log(this.print_data());
    return 'this';
  }
}

module.exports = MyPro1;

src/externals/polis.js

const MyPro1 = require('../core/sample1');
const MyPro = require('../utils/usable');

class MyRock { 
  constructor() { 
    this.content = new MyPro1();
  }

  call_methods() { 
    let data = MyPro.smaple_data('100', this.content.smaple_data, null);
    console.log(data);
  }
}

const x = new MyRock();
x.call_methods();

src/utis/usable.js

class MyPro {

  static smaple_data(data, method, call) {
    method();
    return data;
  }
}

module.exports = MyPro;

I am calling utils and core js files inside it. It throws error but don't know how to solve this error.

Traceback
console.log(this.print_data());

TypeError: Cannot read property 'print_data' of undefined
utks009
  • 573
  • 4
  • 14
  • If I'm not wrong, the context is lost with `this.content.smaple_data`, you probably need `() -> this.content.smaple_data()` instead (or arrow functions). – sp00m Apr 27 '20 at 10:27
  • @sp00m Can you please elobrate I'm new to this. – utks009 Apr 27 '20 at 10:35
  • sp00m is correct. Btw, `MyPro` probably should be a simple object literal not a `class`. – Bergi Apr 27 '20 at 10:43
  • @utks009 It's all very well explained in https://stackoverflow.com/a/20279485/1225328 already :) – sp00m Apr 27 '20 at 16:40

0 Answers0