Calling up functions from another class in es6

a.anev

My problem is very simple. Basically, I have a Controller and a View class. When I click a button, the controller tells the view to display a thing. Problem is, the Controller can't. Here's the code.

class Controller {

    constructor(view) {
        view = new View();
      
        let button = document.getElementById('button');
        button.addEventListener('click', () => {
            controller.doThing();
        });
    }
    
    doThing() {
        view.drawThing(5, 5);
    }

}

class View {
    
    constructor(controller) {
        let canvas = document.getElementById('canvas');
        let pen = canvas.getContext('2d');
        
        this.controller = Controller;
        this.drawThing = drawThing();
    }

    drawThing(x, y) {
        pen.beginPath();
        pen.moveTo(0, 0);
        pen.lineTo(x, y);
        pen.stroke();
    }

}

The result of which is an

Uncaught TypeError: Cannot read property 'drawThing' of undefined
at Controller.doThing (Controller.js:17)
at HTMLButtonElement.Controller.button.addEventListener (Controller.js:12)
Michał Perłakowski

The view variable is scoped only to the constructor. You should use this.view instead:

class Controller {

    constructor(view) {
        this.view = new View();

        let button = document.getElementById('button');
        button.addEventListener('click', () => {
            controller.doThing();
        });
    }

    doThing() {
        this.view.drawThing(5, 5);
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Tkinter multiprocessing and calling functions from another class

From Dev

Calling class functions from another class function with JavaScript

From Dev

React / ES6 - Why calling a function inside another only works with es6 arrow functions?

From Dev

Calling ES6 class constructor from class static method

From Dev

Calling functions in ES6

From Dev

ES6 functions, arrow functions and 'this' in an ES6 class

From Dev

Calling Volley from another class

From Dev

Calling Dialog from another class

From Dev

Calling an object from another class

From Dev

Calling a function from another class?

From Dev

ES6 Javascript: Calling static methods from classes with arrow functions

From Dev

Calling another JFrame/JPanel from another Class

From Dev

Calling up a return value from another module

From Dev

Calling a class from another class with main method

From Dev

PHP - Calling database class from another class

From Dev

Calling function from one class in another class

From Dev

Calling a method from one class in another class

From Dev

Calling functions from within another - javascript

From Dev

Parsing ES6 Class Objects From localStorage Doesn't Include Class Functions

From Dev

Assign multiple functions to class in ES6

From Dev

SwiftUI Calling functions from other class

From Dev

Calling Functions from within class using CTypes

From Dev

MATLAB: OOP Calling Functions from Different Class

From Dev

calling <reified T> inline functions from a class

From Dev

Calling a function from a module in ES6

From Dev

Traversing up a PHP class structure by calling parent functions recursively

From Java

Calling a Spring service class from another

From Dev

Calling function from another class (React)

From Dev

Calling a method from inside of another class

Related Related

  1. 1

    Tkinter multiprocessing and calling functions from another class

  2. 2

    Calling class functions from another class function with JavaScript

  3. 3

    React / ES6 - Why calling a function inside another only works with es6 arrow functions?

  4. 4

    Calling ES6 class constructor from class static method

  5. 5

    Calling functions in ES6

  6. 6

    ES6 functions, arrow functions and 'this' in an ES6 class

  7. 7

    Calling Volley from another class

  8. 8

    Calling Dialog from another class

  9. 9

    Calling an object from another class

  10. 10

    Calling a function from another class?

  11. 11

    ES6 Javascript: Calling static methods from classes with arrow functions

  12. 12

    Calling another JFrame/JPanel from another Class

  13. 13

    Calling up a return value from another module

  14. 14

    Calling a class from another class with main method

  15. 15

    PHP - Calling database class from another class

  16. 16

    Calling function from one class in another class

  17. 17

    Calling a method from one class in another class

  18. 18

    Calling functions from within another - javascript

  19. 19

    Parsing ES6 Class Objects From localStorage Doesn't Include Class Functions

  20. 20

    Assign multiple functions to class in ES6

  21. 21

    SwiftUI Calling functions from other class

  22. 22

    Calling Functions from within class using CTypes

  23. 23

    MATLAB: OOP Calling Functions from Different Class

  24. 24

    calling <reified T> inline functions from a class

  25. 25

    Calling a function from a module in ES6

  26. 26

    Traversing up a PHP class structure by calling parent functions recursively

  27. 27

    Calling a Spring service class from another

  28. 28

    Calling function from another class (React)

  29. 29

    Calling a method from inside of another class

HotTag

Archive