Created a module:

var module= (function () { //мой объект. var object = []; return { //функции... } })(); 

Why can't I access this object in another file? I module.object so: module.object but writes that not known variable. How to be? Can you tell me something.

  • You have the wrong comment at the end. - Pavel Mayorov
  • There should be no “features”, but “public properties”. - Pavel Mayorov pm
  • Your object not a public property - and therefore not visible from the outside. - Pavel Mayorov pm

1 answer 1

The var keyword in javascript declares a local variable that is visible only inside a function. To declare a property that is visible from the outside, you need to do this:

 var module = (function() { return { object: [] }; })(); 

But I would recommend to make the module a little different:

 var module = new function() { this.object = []; } 
  • oh no, in the first example, it’s not through this that otherwise it will be added to the global object - Grundy
  • @Grundy Everything is fine. In the normal mode it is added to the function, in the strict this===undefined . The second option works correctly in both modes. - user194374
  • 2
    No, in the normal mode, just to the global object, because without new it is called, respectively, in the exception of the exception - Grundy
  • one
    @kff in the first fragment: this.object = []; - here this is window , respectively, module - also window - Igor
  • 2
    @kff, well, here's an example to be sure :-) jsfiddle.net/pcdqy1m3/3 - Grundy