6

I am trying to understand a third party Javascript code. But am not able to figure out what is the use of the below coding style.

 function A(){
    }
A.Prop = '23';
A.generate = function(n){
   // do something
}

And then it is just used as :

A.generate(name);

Can someone explain what this code is doing. I understand some bit of OO Javascript, but i wonder if this is any other form of extending an object with new properties to it. Though i dont see any "new" keyword being used, to create an object.

Any ideas ?

Thanks,

3 Answers 3

7

They are creating a namespace. There are many ways to do this, and all are more-or-less equivalent:

A = {
    Prop : '23',
    generate : function (n) {
        // do something
    }
};

Or, equivalently:

A = { };
A.Prop = '23';
A.generate = function (n) {
    // do something
};

Also, if you like being verbose:

A = new Object();
A.Prop = '23';
A.generate = function (n) {
    // do something
};

function is usually used to denote a "class" rather than a "namespace", like so:

A = (function () {
    var propValue = '23';    // class local variable
    return {
        "Prop" : propValue,
        "generate" : function (n) {
            // do something
        }
    };
})();
// then I can use A in the same way as before:
A.generate(name);
1
4

It looks like they're using a dummy function to create a namespace.

You're right; this is useless.
They should use a normal object instead.

7
  • Thanks, for the answer, i was thinking on the same lines. But do you know will there be any performance issue, using this coding style. I mean to say is this a general way to create namespaces. I would assume code var A = new function(){}(); code to do something like this, if i dont touch the whole codebase ?
    – duskandawn
    Apr 5, 2011 at 1:44
  • 1
    The correct way to create a namespace is to use an object literal: var a = { };.
    – SLaks
    Apr 5, 2011 at 1:52
  • 1
    Unless they want to support "instances" of that object, right? function A(){} allows for the var a = new A(); pattern, I think.
    – Andrew
    Nov 5, 2012 at 22:03
  • @Andrew: But they're not making a class.
    – SLaks
    Nov 5, 2012 at 22:11
  • 1
    @SLaks: Isn't that assuming the conclusion? How do you know that's being used as a namespace without more context?
    – Andrew
    Nov 6, 2012 at 16:02
0

A function is an object, there's nothing inherently wrong with using it the way it's been used. However, since the function isn't actually used as a function, it would be better to use an Object. You could also use an Array (which is an object), but the same advice applies.

Also, identifiers starting with a capital letter are, by convention, reserved for constructors (unless they are all capitals, which are, by convention, for constants) so use a name starting with a lower-case letter.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.