R
R
Roman Tatarinov2018-04-21 20:30:17
JavaScript
Roman Tatarinov, 2018-04-21 20:30:17

How to competently organize JS code in OOP style separated by files?

I can't fully understand the structure of how OOP code should act in order to access the parent properties of a constructor. It is not clear where to create and call objects.
Let's say I have two classes, this is `app` - my main class, `map` - a class with a map, which is a child of the class `app`
I have the following class code `app`

class App {
    constructor() {
        this.app = document.querySelector('#city');
    }

    init() {

    }
}

const app = new App();

app.init();

export default function () {
    return App;
}

From here, I export the entire class so that I can access its constructor in another file. The code is below
import App from '../index.js'; //забираю мой класс
class Map extends App {
    constructor() {
        super();
        console.log(this.app); //здесь почему то приходит undefined??
    }
   
   // ... my metods
}

The question is why `undefined`? And the second question is where you need to create an object from the `map` class. That is, I need to export the class and then create an object in the parent and access the methods? or in some other way? In this case, you need to take into account that the `map` class will also have subclasses.
I will be very grateful, if someone throws off an example. Or at least a link to a well-organized code to look at.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2018-04-21
@SilenceOfWinter

JS is far from Java and OOP in it is so-so. See how it is implemented in frameworks, like Angular is well known to everyone, I personally liked EmberJS - it has a good manual.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question