5

I want to write a line like i do in c#

namespace abc.def.ghi{

   get1();
   get2();
   get3();
   get4();    
}

to reduce my text in so many line like

abc.def.ghi.get1();
abc.def.ghi.get2();
abc.def.ghi.get3();
abc.def.ghi.get4();

Is it possible in javascript?

3 Answers 3

3

Objects. The way I learnt anyway

var abc = {};
abc.def = {};
abc.def.ghi = {};
abc.def.ghi.get1 = function(){};
abc.def.ghi.get2 = function(){};
abc.def.ghi.get3 = function(){};
abc.def.ghi.get4 = function(){};

Edit

May have misunderstood the question. If you want to reduce the amount of typing needed, then go with

var ns = abc.def.ghi;
ns.get1();

etc

Sign up to request clarification or add additional context in comments.

2 Comments

If you remove the () after get* in your first example, it is correct ;)
Yup, sorry, just noticed that myself :)
1

This is possible with some custom code:

First define the namespace function and named object function like so:

     var jaf = {}; // define a top level global object for your library


     /**
      * Creates a named Object with a <tt>name</tt> field which is assigned the
      * <tt>strName</tt> and a toString method
      * @private
      * @param {String} strName The name of the named object
      */
     function namedObject(strName)  {
        return {
           name: strName,
           toString: function() {
              return this.name;
           }
        };
     }



     /**
      * Createa new namespace(s) globally or returns existing ones if already created. Namespaces
      * can be used to avoid creating a lot of global objects and structuring a project's
      * modules and classes. Namespaces are like packages in java. The namespace is a
      * simple string or a dot separated string of characters that are allowed in identifiers
      * e.g. "jaf.core" is a valid namespace but "jaf.1" is not.
      * @param {String} strNs The namespace string
      * @return {Object} The namespace object
      */
     var namespace = function(strNs) {
        var arrNsc = strNs.split(".");
        var nsObj = null;
        var i = 0;
        var len = arrNsc.length;

        var nsName = "";

        if(arrNsc[0] === "jaf")  {
           nsObj = jaf;
           i = 1;
           nsName = "jaf.";
        }

        for(; i < len; i++)  {
           var ns = arrNsc[i];
           nsName += (ns + ".");

           if(!nsObj) {
              if(!window[ns])   {
                 nsObj = window[ns] = namedObject(nsName.substring(0, nsName.length - 1));
              }else {
                 nsObj = window[ns];
              }
           }else {
              if(!nsObj[ns])   {
                 nsObj = nsObj[ns] = namedObject(nsName.substring(0, nsName.length - 1));
              }else {
                 nsObj = nsObj[ns];
              }
           }
        }

        return nsObj;
     }

Then you can do:

     var ns = namespace("jaf.core.util");
     ns.MyUtil = function() {
        // do something important
     }

With if you change the variable "jaf" as global object make sure you change the namespace function with appropriate variable. But still you can also do something like:

   var ns1 = namespace("abc.def.ghi")
   ns1.get1() = function() {}
   ns1.get2() = function() {}
   ns1.get3() = function() {}

It will still work this way.

Comments

0

You could use the with statement:

with (abc.def.ghi){    
   get1();
   get2();
   get3();
   get4();    
}

But:

Note: While using a with statement can make your program more concise, improper use of with can significantly slow down your program.

Performance wise it would be better to just store the reference of the object in a local variable as Psytronic suggests.

4 Comments

When I learnt closures, all the articles I read were using with, so I'd be interested to know if it should be avoided too.
@Psytronic: Afaik, with creates a new scope containing the properties of the given object at the beginning of the scope chain. So every access to variables that are not properties of the object takes longer (even access to local variables). So it has to be used with care.
It should be avoided, there's only a few (maybe only one), where it's applicable and okay to use it, that's in for loops and calling functions within it using the iterator int, see here for more answers and why it should be avoided: stackoverflow.com/questions/61552/…
Ah ok, I was told it just bought whatever was within the brackets to the top of the scope chain, I didn't know it created a new scpoe. I've never used it anyway, never had a need to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.