P
P
Programep2016-05-28 17:05:02
JavaScript
Programep, 2016-05-28 17:05:02

how to deal with namespace in javascript?

I don't quite understand how to use namespaces in javascript.
Tell me why the highlighted line is needed or is not needed at all in this code?

var app = app || {}; // эта строка
function app(){}
app.prototype={
init: function (param){
},
connect: function(){
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-05-28
@alexey-m-ukolov

Tell me why the highlighted line is needed or is not needed at all in this code?
Since there are no native namespaces in js, this is the way to "emulate" them. But given that the function of the same name is then declared, inside which the prototype is also redefined, the above code is complete crap.
It is supposed to be something like this:
var app = app || {};

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

But this approach is, in fact, already outdated, because native modules appeared in ES6. And directories act as namespaces.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question