Static constructor with inner function creates a closure?

Go To StackoverFlow.com

0

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
        };
    };

Update for answer (1.) below.

    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.

2012-04-04 19:17
by Vinyl Windows
"If I call a static function like below" -- I don't see any function call in your code =( Other parts of a question are not so clear too - kirilloid 2012-04-04 19:21


0

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.

2012-04-04 19:30
by jfriend00
Thank you for clearing that up. I've seem to have grasped that. for the static property like Persons.List=[] I was not clear on my question. For a situation where I've got one collections object, like the array. And a lot of static functions that operate on that one array. I keep trying to picture a way to work with it like. protect static _persons=[]; I will update my code above to reflect what I am talking about - Vinyl Windows 2012-04-04 23:56
Ads