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,

duskandawn
  • 646
  • 1
  • 10
  • 19

3 Answers3

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);
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • 1
    well you should read this book for more professional patterns http://addyosmani.com/resources/essentialjsdesignpatterns/book/ – ncubica May 31 '13 at 21:08
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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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 05 '11 at 01:44
  • 1
    The correct way to create a namespace is to use an object literal: `var a = { };`. – SLaks Apr 05 '11 at 01: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 05 '12 at 22:03
  • @Andrew: But they're not making a class. – SLaks Nov 05 '12 at 22:11
  • @SLaks, what specifically makes you sure of that? – Andrew Nov 06 '12 at 15:16
  • @Andrew: They're only using it as a namespace. – SLaks Nov 06 '12 at 15:18
  • 1
    @SLaks: Isn't that assuming the conclusion? How do you know that's being used as a namespace without more context? – Andrew Nov 06 '12 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.

RobG
  • 142,382
  • 31
  • 172
  • 209