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.