D
D
dom1n1k2015-11-18 18:51:13
JavaScript
dom1n1k, 2015-11-18 18:51:13

Can classes be combined into namespaces in ES6?

I really like the architecture and organization of the Leaflet library. It includes a lot of entities (maps, layers, markers, balloons, vector elements, and so on). But all this sits in a common object, which plays the role of a namespace. Very clear, concise, convenient.

L.Map
L.TileLayer
L.Marker

How to do the same in ES6 or something similar? So that you can then write:
var a = new MyLib.Dedka();
var b = new MyLib.Babka();
var c = new MyLib.Repka();

It's just that the Dedka and Repka classes, which are not clear how and what they relate to, are somehow not very good. Names like MyLibDedka are also not a fountain.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-11-18
Protko @Fesor

But all this sits in a common object, which plays the role of a namespace. Very clear, concise, convenient.

And they do this because there are no modules. There are modules in ES6 and it makes no sense to make such casts anymore. Compare:
import L from 'leaflet';

var m = new L.Map();

// и
import {Map} from 'leaflet';

var m = new Map();

In general, no one forbids you to do this. After all, this is all just a wrapper over Object.create and good old constructors.

K
Konstantin Kitmanov, 2015-11-18
@k12th

What's stopping you from doing this?

// MyLib.js
export class Dedka {}
export class Repka extends Dedka {}

// клиентский код
import MyLib from './lib/MyLib';

const a = new MyLib.Dedka();
const c = new MyLib.Repka();

Another thing is that it makes sense, if possible, to write
import {Dedka, Koshka as Zhoochka} from './lib/MyLib';
not a penny. Sergei Protko is absolutely right, all these pseudo-"namespaces" are solely due to the lack of a normal modular system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question