0

I'm quite new to Javascript, being used to C++. I'm trying to use an object to create a namespace, populating it with other objects and functions. The problem is that I can't access a property of the namespace while I call the constructor of an object within the namespace, while I can do it from a function withing the namespace. This is an example:

class A 
{
  /*::nome:string;*/
  /*::file:string;*/
  constructor(nome/*:string*/, file/*:string*/, vector/*:A[]*/)
  {
      this.nome=nome;
      this.file=file;
      vector.push(this);
  }
}

class B 
{
  /*::nome:string;*/
  /*::file:string;*/
  constructor(nome/*:string*/, file/*:string*/)
  {
      this.nome=nome;
      this.file=file;
      //vector.push(this);
  }
}

var NS=
{
  namechecker: [],
  alfa: new A("nameA", "fileA", this.namechecker),
  beta: new B("nameB", "fileB"),
  check_names()
  {
      for (var i/*:number*/=0; i<this.namechecker.length; ++i)
      {
          for (var j=i+1; j<this.namechecker.length; ++j)
          {
              // some code to be called later to check that the names and the 
              // filenames are not the same for different objects
          }
      }
  }       
}

At this point flow tells me, regarding the object NS.a : "Cannot get this.namechecker because property namechecker is missing in global object [1]."

While N.b works fine. And N.check_names can apparently access the property "namechecker" without problems of any sort.

Any idea of what am I doing wrong with this code (and not about my life or my idea to use JS to code)?

Thanks.

Edit: as suggested by @Bergi who pointed to another question (marking this as duplicate) the solution is a simple workaround:

var NS=
{
  namechecker: [],
  check_names()
  {
      for (var i/*:number*/=0; i<this.namechecker.length; ++i)
      {
          for (var j=i+1; j<this.namechecker.length; ++j)
          {

          }
      }
  },
  init()
  {
    this.alfa= new A("nameA", "fileA", this.namechecker);
    this.beta= new B("nameB", "fileB");
    this.check_names();
  }      
}

Which, indeed, gives no problem. Thanks.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
DrHell
  • 461
  • 3
  • 17
  • 1
    why are you passing `this.namechecker` to `new A` and not just `namechecker`? you should read about the [context of `this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) – Sagiv b.g Nov 11 '18 at 16:46
  • 1
    Also note that "namespace objects" are an outdated concept. If you're starting something fresh, consider using [modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) instead (via a bundler like Webpack or Rollup, or even natively [in modern environments, though even with native support, there are use-cases for bundling anyway]). – T.J. Crowder Nov 11 '18 at 16:48
  • @Sagivb.g because else Flow complains that: "Cannot resolve name `namechecker`." Interesting enough, it complains again only when I try to access namecker within "a" constructor call and not within the function check_names (which works fine iboth with or without the "this"). – DrHell Nov 11 '18 at 16:49
  • 1
    Re your `namechecker` array, you might want to look at [`WeakSet`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet) so instances can get cleaned up when they're not needed anymore, if you just want to know if they came from your code, or [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) if you plan to associate more information with them. – T.J. Crowder Nov 11 '18 at 16:51
  • @T.J.Crowder Thanks, I will look that. Even if in this case it might be an overkill: the elements in the array are there to stay (practically they are some sort of constants which will be an easy mnemonic way to access some filenames and other data). (About namespaces and import, I don't want to get too far deep in Javascript concepts. Just think that my first idea was to write the code in C++ and then migrate it with Emscripten, but then I opted for Javascript because -even with the source map and all- it seemed less problematic to debug) – DrHell Nov 11 '18 at 17:00
  • 1
    @DrHell - Ah, if they're *meant* to endure, that's different. :-) – T.J. Crowder Nov 11 '18 at 17:02

0 Answers0