1.)If I call a static function like below and have to perform an additional operation on some data passed into the static function. Will calling a nested an inner function create a closure? What I am after is to keep the operations like MakeSomethingOk unavailable to the global scope, since the MakeSomethingOk is only relevant to the Person.PerformSomeTypeOfOperation
2.) Is there anyway to access a collections object like _persons[] from other static function constructors, as in shared? Without having to do a Persons._persons
3.) Is a function constructor still referred to a a constructor if the function is static?
function Person() { };
function Persons() { var _persons = []; };
Person.PerformSomeTypeOfOperation = function (someThing) {
if (someThing == 'turnsOutToBeOk') { return 'anyThing' }
else {
function MakeSomethingOk() { }
//someThing is now being made Ok
};
};
function Person() { };
function Persons() { **protected static** _persons = []; };
Person.Operation1 = function (person) {
if(person.id == _persons[0].id){}//do something
};
Person.Operation2 = function (person) {
if(person.height > 7){//do something, like call NBA
_persons.push(person);
}
};
Person.Operation3 = function (person) {
if(person.isHungOver){//do something, like call AA
_persons.slice(3,1);
}
};
So I am trying to access the same static field without exposing it to the public, and still be able to work with the list object.
Local functions are perfectly fine (functions defined within another function) and they are used for a couple reasons. First, they do limit the scope that they can be called to within the function they are defined in. Second, they have access to the arguments and local variables of the parent function:
Person.PerformSomeTypeOfOperation = function (someThing) {
function MakeSomethingOk() { /* code here */ }
if (someThing == 'turnsOutToBeOk') { return 'anyThing' }
else {
MakeSomethinngOK();
};
};
One would typically define them at the beginning of the parent function because the javascript interpreter will "hoist" them to that location anyway.
As to your second question, static functions can have static data. So, you could have Persons.list = [] and then anyone could access that data as Persons.list, including other functions. If you want private data that only the member functions can access, then you will have to use a different construct like here.
Your third question is pretty much just a semantic question. Usually the point of a constructor is to create/initialize an object. If you're only going to have a singleton (one static object), I think of it more as a one time initialization rather than a constructor that could be used for multiple objects, but that's really just my opinion - I don't think there's an exact definition that distinguishes between create/initializing a singleton vs. multiple objects in this context.