1

I'm trying to document some of my JavaScript according to this JavaScript Documentation guide and came across member access (private, public, protected). I was wondering what the classification would be of a member/function that was defined in the child class but only available in the child class's and it's parent's scope similar to the code snippet below.

Right now the methods initialize and returnItems are declared as public members inside homeBrew.queries.documentLibrary, but they aren't accessible outside of the homeBrew.queries class. Would they then be considered private, since they aren't globally accessible?

var homeBrew = homeBrew || {};

var homeBrew.queries = function () {
    var documentLibrary = function () {
        var siteContext = null;

        var initialize  = function (url, viewName) { /* ... */ }
        var getContext  = function () { /* ... */  }
        var sortItems   = function () { /* ... */  }
        var returnItems = function (callback) { /* ... */ }
        return {
            init: initialize,
            returnItems: returnItems
        }
    }

    var listLibrary = function () {
        // ...
        return { /*...*/ }
    }

    var libraryType = null;
    var queryDocumentLibrary = function (url, viewName) {
        libraryType = 'Document'
        documentLibrary.initialize(url, viewName)
    }
    var queryListLibrary = function (url, viewName) {
        libraryType = 'list'
        listLibrary.initialize(url, viewName)
    }
    var returnItems = function (callback) {
        switch (listType){
            case 'library':
                documentLibrary.returnItems(callback);
                break;
            case 'list':
                listLibrary.returnItems(callback);
                break;
            default:
                console.log('No query executed');
        }
    }

    return {
        queryDocuments: queryDocumentLibrary,
        queryLists: queryListLibrary,
        returnItems: returnItems
    }

}

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
KGlasier
  • 219
  • 1
  • 6
  • 2
    Aren't these [closures](https://en.wikipedia.org/wiki/Closure_(computer_programming))? – candied_orange Jul 31 '19 at 19:38
  • @candied_orange they might be, but since I have to work with IE a lot, true classes (which were introduced with ES6) don't work. So I found [this module/namespace](https://github.com/stevekwan/experiments/blob/master/javascript/module-pattern.html) method to emulate classes to a degree. – KGlasier Jul 31 '19 at 20:19
  • Have you considered an ES6 shim? – kevin cline Jul 31 '19 at 20:28

3 Answers3

1

In general, the visibility attributes are as follows:

Private: Only visible to this class.

Protected: Visible to this class and to any other class that is derived from it. Used for "how it works" methods, "for internal use only."

Public: Visible to everyone, and intended to be used by anyone.

Mike Robinson
  • 1,765
  • 4
  • 10
0

Ordinarily, this would be handled by placing an abstract declaration in the parent class. This says that children of this class are required to provide a "concrete" implementation of a method, but that the parent itself does not.

Consider e.g. this page, which discusses the concept of "abstraction" in general. (You will encounter this same concept in many programming languages, although of course the implementations differ.)

Mike Robinson
  • 1,765
  • 4
  • 10
0

Right now the methods initialize and returnItems are declared as public members inside homeBrew.queries.documentLibrary, but they aren't accessible outside of the homeBrew.queries class.

homeBrew.queries is not a class. It is an object. In fact, homeBrew.queries is an object returned by a function closure. This makes the concepts of private, protected and public a bit muddy, because functions in JavaScript are both functions and objects. How you call them and reference them determines who has access.

To be honest there is no direct translation between a classical object oriented language and JavaScript here, because the function closure creates another scope by which you can refer to and call the initialize and returnItems functions.

I do not actually see any parent or child classes in the code. I see procedural code organized into objects serving as a kind of namespace hidden by a function closure. Pretty standard fare in JavaScript, really.

True inheritance in JavaScript — including the concept of classes — is achieved using prototypal inheritance. Notice I said the concept of classes. Prototypal languages do not have classes in the sense of languages like Java, C# or C++. JavaScript has no classes. It just has objects used as prototypes for new objects rather than classes, which serve as blueprints for new objects.

The combination of function closures and a lack of prototypal inheritance means the original premise of the question is false. There is no correlation to private, protected and public access. There is only public access and function scope.

Greg Burghardt
  • 34,276
  • 8
  • 63
  • 114