Namespaces in Javascript
-
Oops, my bad, I am talking about the upcoming 646 (I'll upload it in the next days)
-
I thought so but wanted to check before I re-downloaded the current build
-
Alright, Build 646 is online.
-
That was quick :D
-
Can
this
be used within a namespace to refer to its own functions? -
Nope,
this
is reserved for object properties. -
What about creating another keyword like
self
? -
Adding Python to the melting pot, I like it :)
Why do you need this? I would indeed prefer adding a new keyword like
self
, but I can't see any advantage yet. -
If we're at it, what do you think about a
private
keyword?namespace MyEncapsulatedStuff { private var internals = "Hidden"; inline function getHiddenProperty() { return self.internals; // :) }; }; Console.print(MyEncapsulatedStuff.internals); // undefined Console.print(MyEncapsulatedStuff.getHiddenProperty()); // "Hidden"
It would solve the problem of the weird Javascript encapsulation tactics.
-
A
self
keyword will be helpful for example when I have functions within a namespace that refer to each other and to private namespaced variables. Rather than writing out the namespace's name I could just putself
and if I decide to change the namespace name I won't need to adjust the function and variable references within the namespace. Theprivate
keyword is a good idea :) -
You could also skip the
namespace.
and write the variable name without prefix. This will work even if there are global variables with the same name.