How to work with namespace in javascript?

Don't quite understand how to use namespaces in javascript.
Tell me why you need a dedicated line or are not necessary in this code?
var app = app || {}; // this line
function app(){}
app.prototype={
init: function (param){
},
connect: function(){
}
}
July 9th 19 at 10:57
1 answer
July 9th 19 at 10:59
Tell me why you need a dedicated line or are not necessary in this code?
Because js has no native namespaces, use this method of "emulation". But considering that then the same function is declared inside the prototype is overridden, the code - complete crap.

It is expected that there will be something like this:
var app = app || {};

app.myModule = (function () {
 connect: function(){}
})()


But this approach, in fact, already outdated, because in the appeared native ES6 modules. And as namespaces are directories.
But then will not work
var app=new app();
in index.html
The fact that trying to understand OOP in js, and to do it immediately more or less supported code. Before that started out just functions in index.html to write. Now put everything in a separate js file and there trying to make a more or less clear structure, properties and functions. Can suggest how best to structure js code? - mikayla_Skil commented on July 9th 19 at 11:02
Once you are just starting to learn js - take from native ES6 modules. - aniya commented on July 9th 19 at 11:05
here you can bet. >50% haven't upgraded to ES6 - blake_Eichma commented on July 9th 19 at 11:08
: I work with extjs. In pure js almost did not write, so the PLO was not working. This standard has recently been released. Think it is too early to use it. - mikayla_Skil commented on July 9th 19 at 11:11
: Yes, by itself it is still not widely available, but there are transpapillary that compile compatible with all modern browsers code.
Don't want to use it - there are several other implementations of the modules. Namespaces in js are not needed particularly in the presence of modules: import what we need and call variable as you want - no conflicts. So, forget about namespaces and immerse yourself in the essence of OOP in js in your code shows that you don't quite understand him. - aniya commented on July 9th 19 at 11:14
But then will not work
var app=new app();

1. Constructor functions should be named with a capital letter.
2. If you need new App(), then what's namespaces? - aniya commented on July 9th 19 at 11:17

Find more questions by tags OOPJavaScript