Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this an ordinay function, a constructor or neither?

I use to enjoy using js. Now I am just frustrated. I am trying to jsDoc a large chunk of code that was purchased by my boss. The author did not provide an API so I am going to create one using jsDoc.

Unfortunately the code is not clear and it contains about 10k lines of code in one js file. I have used object literals in the past, this is not an object literal. The parameters and functions are not separated by commas. Additionally, a prototype is never used, but the new operator is. I have looked in several books and namespacing appears to be the closest 'style'. Here is the generic flow of the program. My questions follow the example.

(function () {
var global = (function () {return this; } ).call();
global.X = {};
})();

X.init = function(){
    X.MyObjectA = new X.FooA();
    X.MyObjectB = new X.FooB();
}

X.FooA = function() {
   var storeStuff;
   do stuff
   .
   .
   function bar(){
        var storeSomething;
        do something
        do somthing else
        .
        .
        moreFunctions(){}
        var moreVars;
        .
        .

    }

    this.anOccasionalFunctionLiteral = function(){}
        .
        .
}

X.FooB = function(){
    Similar contents of FooB()
 }
 .
 .
 .
 Continues forever...

So my questions are:

  1. What style is this!? When I say style I guess I really mean what are FooA and FooB? Are they objects? Classes?

  2. What title should I give the nested functions if they aren't members of X.FooA or X.FooB. I have been told nested functions, including bar(), are not methods of objects FooA and FooB.

  3. If the function nested inside of the object is in fact not a method of its surrounding object can it be called from anywhere? Or do normal scope rules apply?

To make life more readable and happy I have been classifying objects similar to FooA and FooB as constructors and any methods inside of the constructors, such as bar(), as methods of FooA and FooB. This may not be correct but at least it's organized. Any suggestions?

Thanks for your help!

like image 708
atomSmasher Avatar asked Jan 26 '26 08:01

atomSmasher


1 Answers

FooA and FooB are constructor functions.

The nested functions are "private" functions.

The bar function inside of FooA can only be called inside FooA.

See http://javascript.crockford.com/private.html

like image 123
Aaron Kurtzhals Avatar answered Jan 27 '26 21:01

Aaron Kurtzhals



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!