What do you call a set of Javascript closures that have a common context?

I'm trying to learn closure (in Javascript), which is why I have been hurting my brain for too long with C # and C ++. I think I now have a basic understanding, but one thing bothers me: I visited many sites in this Knowledge Search, and I havenโ€™t seen a single word (or even a simple two-word phrase) anywhere, which means a Javascript closure set that has a common execution context. " For instance:

function CreateThingy (name, initialValue)
{
    var myName = name;
    var myValue = initialValue;

    var retObj = new Object;

    retObj.getName = function () {return myName; }
    retObj.getValue = function () {return myValue; }
    retObj.setValue = function (newValue) {myValue = newValue; }

    return retObj;
};

From what I read, this is apparently one of the common ways to implement data hiding. The value returned by CreateThingy is, of course, an object, but what would you call a set of functions that are properties of this object? Each of them is a closure, but I would like a name that I could use to describe (and think) all together as one conceptual entity, and I would rather use a common name than do this.

Thank!

- Ed

+5
source share
3 answers

Crockford popularized the term " privileged method " to name functions that have access to the area of โ€‹โ€‹his constructor (where private members are declared).

" JavaScript, ".

" " , , " ", , this , " " " ", , .

+3

. :

var thingy = (function(specObj) {
    var my = {},
        privateVar = blah,
        someOtherPrivateVar = [];

    my.getSomeVal() {
        return specObj.someVal;
    }
    my.getAnotherVal() {
        return specObj.AnotherVal;
    }
}({
    someVal: "value",
    anotherVal: "foo"
}));

I am initialized with an object literal because using a named object will allow me to change the state of things by changing the named object. There are other options for this.

Here's another good link to module template options: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

Almost self-evident, but Crockford Javascript: The Good Parts contains a good attitude to this style and its specific advantages.

0
source

All Articles