Namespaces in Javascript


  • administrators

    Alright, Build 646 is online.



  • That was quick :D



  • Can this be used within a namespace to refer to its own functions?


  • administrators

    Nope, this is reserved for object properties.



  • What about creating another keyword like self?


  • administrators

    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.


  • administrators

    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 put self and if I decide to change the namespace name I won't need to adjust the function and variable references within the namespace. The private keyword is a good idea :)


  • administrators

    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.


Log in to reply

Looks like your connection to Namespaces in Javascript was lost, please wait while we try to reconnect.