Basic knowledge on using Javascript access modifiers and inheritance

Disclaimer: This topic's to provide some extra choices for those needing/wanting some protections from being able to access anything from anywhere while still allowing inheritance. As sometimes it completely makes sense for keeping everything public, these choices are entirely optional. Using any of them without any solid reason's just violating KISS for nothing, which should be avoided.

This topic aims to use an easy, simple and small scenario to briefly cover some patterns illustrating Javascript ES5 access modifiers and inheritance.
Bear in mind that those patterns can quickly become antipatterns if they're used without at least thinking twice.
So you're assumed to have at least a basic knowledge on writing Javascript and have written at least several easy, simple and small js files which work without nontrivial bugs.
The focus of this topic corresponds to 'Remembering' in the new version of the Bloom's Taxonomy.

Please note that the following concepts will be used:
1. Final - Functions/variables that can't be redefined after their initial definitions
2. Private - Functions/variables only accessible by the enclosed class/instance/function
3. Protected - Functions/variables only accessible by the enclosed class/instance and their subclasses/instances
4. Public - Functions/variables accessible from anywhere
5. Static - Functions/variables shared by all instances of the same class

On a side note: Strictly speaking, it's nearly impossible to always ensure an object method will always remain private/protected, as advanced programmers can, after thoroughly comprehended the object's API, reconstruct the whole object while preserving its external behaviors, even though its internal states will most likely be lost that way. However, it's such an unreasonably tedious task for nontrivial objects having nontrivial inheritance hierarchies that only truly trivial and/or valuable objects really worth such tremendous efforts. Therefore, let's just regard them as edge cases and move on.

Warmup
Spoiler
Recall that the only scoping instrument in Javascript is function, which goes hand in hand with closures. They're the very basis of emulating access modifiers in Javascript.
Without functions, anything's accessible from anywhere in Javascript, meaning that everything's public.

On the other hand, using the function scope lets one declare variables inside a function, making them only accessible within that function. These variables are thus local to that function, making them private.

When it comes to emulating protected in Javascript, one must first bear in mind that protected only makes sense when there's inheritance, which nearly always involve this.
Inheritance in Javascript are prototype based, which is different from almost(if not just) all the other programming languages supporting inheritance.
Nevertheless, protected in prototypical inheritance can still mean variables only accessible by the prototype defining them and all the other prototypes having that prototype as their parent.


Final in Javascript can be regarded as const in ES6. Static in Javascript can mean variables shared by all objects inheriting from the same prototype.

With all these in mind, let's get started.

Situation
Spoiler
Suppose we're to create a lockable container that can store a single object as the encapsulated contents and can lock itself to control its access.
This object's API consists of the following:
1. isLocked() - Check whether the lockable container is locked
2. lock() - Locks the lockable container
3. tryPutContents(contents) - Tries to put the contents to the lockable container
It'll succeed only if the lockable container's unlocked and empty, otherwise it'll show the reasons of failure
4. tryTakeContents - Tries to take the contents inside the lockable container
If it succeeds, the lockable container will become empty
It'll succeed only if the lockable container's unlocked and not empty, otherwise it'll show the reasons of failure
5. tryUnlock(key) - Tries to unlock the lockable container
It'll succeed only if the key's correct, otherwise it'll show the reasons of failure

Now the lockable container's easy, simple and small, but some users might fear that eventually an outsider can pass the correct key to unlock the lockable container, take its contents, put something malicious inside, and then lock it again. The only way to know the lockable container's compromised is to inspect its contents, which can itself lead to disastrous results.
So let's create a counted lockable container, which inherits from a lockable container, that counts the number of key mismatches, and permanently locks itself if the counter reaches a preset maximum.
This object has a new method in its API:
6. keyMismatchCount - Returns the number of key mismatches

Now the counted lockable container's noticeably safer, but it has a new problem - Given enough key mismatches, it won't be able to be unlocked anymore, even with the correct key.
So let's create a resettable counted lockable container, which inherits from a counted lockable container, that lets users to reset the key mismatch count.
This object has a new method in its API:
7. tryResetKeyMismatchCount - Tries to reset the key mismatch count to 0
It'll succeed only if the lockable container's unlocked, otherwise it'll show the reasons of failure

Now the resettable counted lockable container's also noticeably safer from being not being able to be unlocked even with the correct key, but some users might just want the reset to take place automatically whenever becomes unlocked.
So let's create a resetting counted lockable container, which inherits from a counted lockable container, that automatically resets the key mismatch count to 0 whenever it becomes unlocked.

With the context in place, the following patterns can finally come into play. All files implementing all these patterns, as well as their unit tests and integration tests, can be found in my Lockable-Container github.

You're highly encouraged and recommended to read the simplest thing that could possibly work first, which is demonstrated by lockableContainerKiss.js.

Wrapped Prototype
Spoiler
It's demonstrated by the following js files:
1. lockableContainerPrototype.js
2. countedLockableContainerPrototype.js
3. resettableCountedLockableContainerPrototype.js
4. resettingCountedLockableContainerPrototype.js

This pattern simply declares the function in the global scope, and everything else, including its prototype, inside an immediately-invoked function expression.
Note that:
1. Anything declared inside the anonymous function but not in the prototype's effectively private static final.
2. This pattern can lead to some duplicated private static constants if it's better than making them instance variables.
3. This pattern doesn't provide private instance variable nor protected function/variable.
4. This pattern causes every subclass to choose its direct parent but not vice versa.

This pattern also generally leads to extremely fast object creation codes using very little memory, and it's very scalable in terms of performance when it comes to extending the prototype chain. It's shown by the below benchmark:
Spoiler
Code:
// All performance tests are done in Google Chrome 56.0.2924.87 and i3-2330M

// Base: Roughly 7MB
// MOE: < 1MB; < 1 sec

// Total: Roughly 25MB for 100,000 times
memoryTest.push(new LockableContainer(_key1));
memoryTest.push(new LockableContainer(_key2));
//
// Total: Roughly 25MB for 100,000 times
memoryTest.push(new ResettableCountedLockableContainer(_key1,
       _maxKeyMismatchCount1));
memoryTest.push(new ResettableCountedLockableContainer(_key2,
       _maxKeyMismatchCount2));
//
// Total: Roughly 25MB for 100,000 times
memoryTest.push(new ResettingCountedLockableContainer(_key1,
       _maxKeyMismatchCount1));
memoryTest.push(new ResettingCountedLockableContainer(_key2,
       _maxKeyMismatchCount2));
//

// Roughly 15 seconds for 100,000,000 times
new LockableContainer(_key1);
new LockableContainer(_key2);
//
// Roughly 17 seconds for 100,000,000 times
new ResettableCountedLockableContainer(_key1, _maxKeyMismatchCount1);
new ResettableCountedLockableContainer(_key2, _maxKeyMismatchCount2);
//
// Roughly 17 seconds for 100,000,000 times
new ResettingCountedLockableContainer(_key1, _maxKeyMismatchCount1);
new ResettingCountedLockableContainer(_key2, _maxKeyMismatchCount2);
//

So this pattern can be desirable when:
1. You don't need/want private/protected instance variables.
2. You don't need/want protected function.
3. You don't mind having duplicated private static constants.
4. You need/want extremely performant object creation codes that scale very well.
5. You need/want/don't mind every subclass to choose its direct parent but not vice versa.

In the case of the lockable container, this pattern doesn't even try to stop outsiders from directly accessing its internals, thus letting them accessing its correct key and contents, making the lockable container meaningless, pointless and useless. This's proved by the integration test as this pattern doesn't pass it. Therefore, this pattern isn't suitable for its implementations at all.

Composable Revealing Module
Spoiler
It's demonstrated by the following js files:
1. lockableContainerFunction.js
2. immutableResettableCountedLockableContainerFunction.js
3. mutableResettableCountedLockableContainerFunction.js

This pattern simply wraps everything into a named function that's called rather than instantiated. It's a special case of the revealing module pattern, as it emphasize using a module as an object rather than a function, with that object effectively adhering to SRP and OCP by keeping it small and composable.
Note that:
1. Anything not returned by the function's private.
2. This pattern can lead to some duplicated private functions/variables.
3. This pattern doesn't provide protected functions/variables.
4. This pattern uses the decorator pattern to extend objects.
5. There's no static function/variable at all.

This pattern generally leads to quite some fast object creation codes when thew object's not extended but those codes uses quite a lot of memory, and it's not scalable at all in terms of performance when the object's extended. It's shown by the below benchmark:
Spoiler
Code:
// All performance tests are done in Google Chrome 56.0.2924.87 and i3-2330M

// Base: Roughly 7MB
// MOE: < 1MB; < 1 sec
// Total: Roughly 151MB for 100,000 times
memoryTest.push(LockableContainerFunction(_key1));
memoryTest.push(LockableContainerFunction(_key2));
//
// Total: Roughly 240MB for 100,000 times
memoryTest.push(MutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key1), _maxKeyMismatchCount1, true,
       false));
memoryTest.push(MutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key2), _maxKeyMismatchCount2, false,
       true));
//
// Total: Roughly 265MB for 100,000 times
memoryTest.push(ImmutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key1), _maxKeyMismatchCount1, true,
       false));
memoryTest.push(ImmutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key2), _maxKeyMismatchCount2, false,
       true));
//

// Roughly 48 seconds for 100,000,000 times
LockableContainerFunction(_key1);
LockableContainerFunction(_key2);
//
// Roughly 91 seconds for 100,000,000 times
ImmutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key1), _maxKeyMismatchCount1,
       isResettable, isResetting);
ImmutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key2), _maxKeyMismatchCount2,
       isResettable, isResetting);
//
// Roughly 17 seconds for 1,000,000 times
MutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key1), _maxKeyMismatchCount1,
       isResettable, isResetting);
MutableResettableCountedLockableContainerFunction(
       LockableContainerFunction(_key2), _maxKeyMismatchCount2,
       isResettable, isResetting);
//

Also, this pattern uses composition instead of inheritance to extend objects. As both a resettable counted lockable container and resetting counted lockable container needs to write the private/protected instance variable counting the number of key mismatches in a counted lockable container, the functionalities of the formers need to be included by the latter in order to extend objects only based on their APIs.

So this pattern can be desirable when:
1. You don't need/want protected function/variables.
2. You don't mind having duplicated private functions/variables.
3. You don't need/want to extend the objects at all or you don't mind suffering from poor scalability.
4. You need/want extremely fast object creation codes, even at the cost of high memory usage, and you don't need/want to extend the objects at all.
5. You need/want private functions/variables.
6. You favor composition over inheritance.

In the case of the lockable container, this pattern can effectively, efficiently and reliably stop outsiders from directly accessing its internals, at least when they're not using tons of ridiculously insane hacks.
Unless exceptionally large amount of lockable containers can exist at the same time, this pattern should be fine.

Parasitic Inheritance
Spoiler
It's demonstrated by the following js files:
1. lockableContainerClass.js
2. countedLockableContainerClass.js
3. resettablecountedLockableContainerClass.js
4. resettingcountedLockableContainerClass.js

This pattern simply wraps everything into a named function that's called by subclasses and instantiated to create new objects.
Note that:
1. Anything not attached to the function's this pointer's private.
2. This pattern can lead to some duplicated private functions/variables.
3. This pattern provides loosely but not strictly protected functions/variables.
4. There's no static function/variable at all.
5. This pattern causes every subclass to choose its direct parent but not vice versa.
6. This pattern supports multiple inheritance, albeit with the diamond problem in the way that the last inherited parent dominates.
7. Subclasses can expose the protected functions/variables into public ones by using accessors.

This pattern generally leads to considerably slow codes that uses quite some memory, and it's not that scalable in terms of performance when the object's extended. It's shown by the below benchmark:
Spoiler
Code:
// All performance tests are done in Google Chrome 56.0.2924.87 and i3-2330M

// Base: Roughly 7MB
// MOE: < 1MB; < 1 sec
// Total: Roughly 116MB for 100,000 times
memoryTest.push(new LockableContainerClass(_key1,
       _maxKeyMismatchCount1));
memoryTest.push(new LockableContainerClass(_key2,
       _maxKeyMismatchCount2));
//
// Total: Roughly 217MB for 100,000 times
memoryTest.push(new ResettableCountedLockableContainerClass(_key1,
       _maxKeyMismatchCount1));
memoryTest.push(new ResettableCountedLockableContainerClass(_key2,
       _maxKeyMismatchCount2));
//
// Total: Roughly 212MB for 100,000 times
memoryTest.push(new ResettingCountedLockableContainerClass(_key1,
       _maxKeyMismatchCount1));
memoryTest.push(new ResettingCountedLockableContainerClass(_key2,
       _maxKeyMismatchCount2));
//

// Roughly 74 seconds for 10,000,000 times
new LockableContainerClass(_key1);
new LockableContainerClass(_key2);
//
// Roughly 12 seconds for 1,000,000 times
new ResettableCountedLockableContainerClass(_key1,
       _maxKeyMismatchCount1);
new ResettableCountedLockableContainerClass(_key2,
       _maxKeyMismatchCount2);
//
// Roughly 13 seconds for 1,000,000 times
new ResettingCountedLockableContainerClass(_key1,
       _maxKeyMismatchCount1);
new ResettingCountedLockableContainerClass(_key2,
       _maxKeyMismatchCount2);
//

Moreover, the protected functions/variables in this pattern can be compromised without too much troubles, as demonstrated by these files:
1. unprotectedLockableContainerClass.js
2. unprotectedCountedLockableContainerClass.js

A serious side effect is that the integrity of the whole object can be compromised, causing it to fail to work correctly.
In this case, such a jailbreak causes the compromised object to use the new versions of the protected functions/variables, while the original codes are still using their old versions.
That's why this pattern doesn't provide strictly protected functions/variables, only loosely protected ones, as it doesn't stop them to be compromised while still letting them express themselves and function as protected ones as long as they're not compromised.


So this pattern can be desirable when:
1. You need/want private and protected functions/variables.
2. You don't mind having duplicated private constants.
3. You don't mind extremely unperformant object creation codes that doesn't scale well.
4. You need/want/don't mind every subclass to choose its direct parent but not vice versa.
5. You don't mind having protected functions/variables compromised, which can compromise the integrity of the whole object as well.
6. You need/want multiple inheritance.
7. You need/want/don't mind subclasses turning the protected functions/variables into public ones.

In the case of the lockable container, whether this pattern's desirable 's mainly determined by the risk of the objects being compromised, which depends on actual use cases.

Reversed Inheritance Hierarchy
Spoiler
It's demonstrated by the following js files:
1. lockableContainerObject.js
2. countedLockableContainerObject.js
3. resettableCountedLockableContainerObject.js
4. resettingCountedLockableContainerObject.js

This pattern simply wraps everything into a named function, including the declaration of all subclasses, that's instantiated to create new objects.
Note that:
1. Anything not attached to the function's this pointer's private.
2. This pattern can lead to some duplicated private functions/variables.
3. This pattern provides strictly protected functions/variables, which are those attached to the function's this pointer but not returned by the function.
4. There's no static function/variable at all.
5. This pattern causes every parent to choose all its subclasses but not vice versa.

This pattern generally leads to exceptionally slow object creation codes that uses a stunning amount of memory, and it's rather not scalable in terms of performance when the object's extended. It's shown by the below benchmark:
Spoiler
Code:
// All performance tests are done in Google Chrome 56.0.2924.87 and i3-2330M

// Base: Roughly 7MB
// MOE: < 1MB; < 1 sec
// Total: Roughly 210MB for 100,000 times
memoryTest.push(new LockableContainerObject('LockableContainer',
       _key1));
memoryTest.push(new LockableContainerObject('LockableContainer',
       _key2));
//
// Total: Roughly 336MB for 100,000 times
memoryTest.push(new LockableContainerObject(
       'ResettableCountedLockableContainer', _key1,
       _maxKeyMismatchCount1));
memoryTest.push(new LockableContainerObject(
       'ResettableCountedLockableContainer', _key2,
       _maxKeyMismatchCount2));
//
// Total: Roughly 329MB for 100,000 times
memoryTest.push(new LockableContainerObject(
       'ResettingCountedLockableContainer', _key1,
       _maxKeyMismatchCount1));
memoryTest.push(new LockableContainerObject(
       'ResettingCountedLockableContainer', _key2,
       _maxKeyMismatchCount2));
//

// Roughly 17 seconds for 1,000,000 times
new LockableContainerObject('LockableContainer', _key1);
new LockableContainerObject('LockableContainer', _key2);
//
// Roughly 31 seconds for 1,000,000 times
new LockableContainerObject('ResettableCountedLockableContainer',
       _key1, _maxKeyMismatchCount1);
new LockableContainerObject('ResettableCountedLockableContainer',
       _key2, _maxKeyMismatchCount2);
//
// Roughly 35 seconds for 1,000,000 times
new LockableContainerObject('ResettingCountedLockableContainer',
       _key1, _maxKeyMismatchCount1);
new LockableContainerObject('ResettingCountedLockableContainer',
       _key2, _maxKeyMismatchCount2);
//

Besides, the control in the inheritance hierarchy's reversed - Rather than letting subclasses to control which classes are their parents, this pattern lets parents to control which are their subclasses. This has the following implications:
1. In the normal inheritance hierarchy, anyone can create subclasses for any public class meant to be inherited; This pattern only lets foreign classes authorized by the classes in this pattern to be the latters' subclasses.
2. In the normal inheritance hierarchy, a class's protected functions/variables can risk being public if it's subclassed by classes exposing them; This pattern can prevent this from happening by strictly controlling the external behaviors of all subclasses.
3. If no one has access to the source code of a class using this pattern, it won't be able to be subclassed by any new class anymore.
4. This pattern isn't maintainable when the inheritance hierarchy becomes colossal, complex and convoluted.

So this pattern can be desirable when:
1. You need/want private and protected functions/variables.
2. You don't mind having duplicated private constants.
3. You don't mind having excessively unperformant object creation codes that lacks scalability.
4. You need/want/don't mind having every parent to choose all its subclasses but not vice versa.
5. You don't mind leading to effectively final classes when no one has access to their source codes anymore.
6. You don't mind managing an unmaintainable mess with a colossal, complex and convoluted inheritance hierarchy.

In the case of the lockable container, this pattern can effectively, efficiently and reliably stop outsiders from directly accessing its internals, at least when they're not using tons of ridiculously insane hacks.
Unless exceptionally large amount of lockable containers can exist at the same time, this pattern should be fine.

Reversed Prototype Chain
Spoiler
It's demonstrated by the following js files:
1. lockableContainerFactory.js
2. countedLockableContainerFactory.js
3. resettableCountedLockableContainerFactory.js
4. resettingCountedLockableContainerFactory.js

This pattern simply wraps everything into a named function, including the declaration of all subclasses and defintion of the prototype for creating objects, that's called once to return a function creating objects.
Note that:
1. Anything not attached to the prototype's private static final function/variable.
2. This pattern can lead to some duplicated private static final functions/variables.
3. This pattern doesn't provide private instance variables.
4. This pattern provides strictly protected functions/variables, which are those attached to the function's this pointer but not returned by the function.
5. This pattern causes every parent to choose all its subclasses but not vice versa.
6. This pattern is largely based on the factory method pattern.
7. The prototype's created only once if this pattern's used properly.

This pattern generally leads to slightly slow object creation codes that uses an acceptable amount of memory, and it's reasonably scalable in terms of performance when the object's extended. It's shown by the below benchmark:
Spoiler
Code:
// All performance tests are done in Google Chrome 56.0.2924.87 and i3-2330M

// Base: Roughly 7MB
// MOE: < 1MB; < 1 sec
// Total: Roughly 60MB for 100,000 times
memoryTest.push(_createdLockableContainerFactory('LockableContainer',
       _key1));
memoryTest.push(_createdLockableContainerFactory('LockableContainer',
       _key2));
//
// Total: Roughly 71MB for 100,000 times
memoryTest.push(_createdLockableContainerFactory(
       'ResettableCountedLockableContainer', _key1,
       _maxKeyMismatchCount1));
memoryTest.push(_createdLockableContainerFactory(
       'ResettableCountedLockableContainer', _key2,
       _maxKeyMismatchCount2));
//
// Total: Roughly 67MB for 100,000 times
memoryTest.push(_createdLockableContainerFactory(
       'ResettingCountedLockableContainer', _key1,
       _maxKeyMismatchCount1));
memoryTest.push(_createdLockableContainerFactory(
       'ResettingCountedLockableContainer', _key2,
       _maxKeyMismatchCount2));
//

// Roughly 21 seconds for 10,000,000 times
_createdLockableContainerFactory('LockableContainer', _key1);
_createdLockableContainerFactory('LockableContainer', _key2);
//
// Roughly 24 seconds for 10,000,000 times
_createdLockableContainerFactory('ResettableCountedLockableContainer',
       _key1, _maxKeyMismatchCount1);
_createdLockableContainerFactory('ResettableCountedLockableContainer',
       _key2, _maxKeyMismatchCount2);
//
// Roughly 25 seconds for 10,000,000 times
_createdLockableContainerFactory('ResettingCountedLockableContainer',
       _key1, _maxKeyMismatchCount1);
_createdLockableContainerFactory('ResettingCountedLockableContainer',
       _key2, _maxKeyMismatchCount2);
//

Similar to the Reversed Inheritance Hierarchy, the control in the inheritance hierarchy's reversed in this pattern as well, leading to the same implications.

So this pattern can be desirable when:
1. You need/want protected functions/variables.
2. You don't need/want private instance variables.
3. You don't mind having duplicated private constants.
4. You don't mind having slightly unperformant object creation codes that's decently scalable.
5. You need/want/don't mind having every parent to choose all its subclasses but not vice versa.
6. You don't mind leading to effectively final classes when no one has access to their source codes anymore.
7. You don't mind managing an unmaintainable mess with a colossal, complex and convoluted inheritance hierarchy.

In the case of the lockable container, this pattern can effectively, efficiently and reliably stop outsiders from directly accessing its internals, at least when they're not using tons of ridiculously insane hacks.
Unless unbelivably large amount of lockable containers can exist at the same time, this pattern should be fine.

Summary
Spoiler
Wrapped Prototype simply declares the function in the global scope, and everything else, including its prototype, inside an immediately-invoked function expression.
This pattern can be desirable when:
1. You don't need/want private/protected instance variables.
2. You don't need/want protected function.
3. You don't mind having duplicated private static constants.
4. You need/want extremely performant object creation codes that scale very well.
5. You need/want/don't mind every subclass to choose its direct parent but not vice versa.

Composable Revealing Module simply wraps everything into a named function that's called rather than instantiated.
This pattern can be desirable when:
1. You don't need/want protected function/variables.
2. You don't mind having duplicated private functions/variables.
3. You don't need/want to extend the objects at all or you don't mind suffering from poor scalability.
4. You need/want extremely fast object creation codes, even at the cost of high memory usage, and you don't need/want to extend the objects at all.
5. You need/want private functions/variables.
6. You favor composition over inheritance.

Parasitic Inheritance simply wraps everything into a named function that's called by subclasses and instantiated to create new objects.
This pattern can be desirable when:
1. You need/want private and protected functions/variables.
2. You don't mind having duplicated private constants.
3. You don't mind extremely unperformant object creation codes that doesn't scale well.
4. You need/want/don't mind every subclass to choose its direct parent but not vice versa.
5. You don't mind having protected functions/variables compromised, which can compromise the integrity of the whole object as well.
6. You need/want multiple inheritance.
7. You need/want/don't mind subclasses turning the protected functions/variables into public ones.

Reversed Inheritance Hierarchy simply wraps everything into a named function, including the declaration of all subclasses, that's instantiated to create new objects.
This pattern can be desirable when:
1. You need/want private and protected functions/variables.
2. You don't mind having duplicated private constants.
3. You don't mind having excessively unperformant object creation codes that lacks scalability.
4. You need/want/don't mind having every parent to choose all its subclasses but not vice versa.
5. You don't mind leading to effectively final classes when no one has access to their source codes anymore.
6. You don't mind managing an unmaintainable mess with a colossal, complex and convoluted inheritance hierarchy.

Reversed Prototype Chain simply wraps everything into a named function, including the declaration of all subclasses and defintion of the prototype for creating objects, that's called once to return a function creating objects.
This pattern can be desirable when:
1. You need/want protected functions/variables.
2. You don't need/want private instance variables.
3. You don't mind having duplicated private constants.
4. You don't mind having slightly unperformant object creation codes that's decently scalable.
5. You need/want/don't mind having every parent to choose all its subclasses but not vice versa.
6. You don't mind leading to effectively final classes when no one has access to their source codes anymore.
7. You don't mind managing an unmaintainable mess with a colossal, complex and convoluted inheritance hierarchy.

I'm planning to open another topic to explain how these pattern works in details, which will lead to a solid understanding on using Javascript access modifers and inheritance.
That's all for now. What do you think about these patterns? What do you think about Javascript access modifiers and inheritance in general? Let's drop your 2 cents here 🙂
I'm not a programmer by education or training or anything so I don't know how much weight anyone should give my ramblings.

Don't hack vocabulary from other languages into JavaScipt (JS). Protected, private, public, final, etc.; toss them all. Those keywords and the phrase "access modifier" have no meaning in JS and shouldn't be used except when the audience has a background in languages that do. A beginner cannot web search for "JavaScript access modifiers" or visit the JS documentation at Mozilla Developer Network to learn more.

Perhaps the intended reader is experienced with other languages but I didn't get that impression from the line "So you're assumed to have at least a basic knowledge on writing Javascript and have written at least several easy, simple and small js files which work without nontrivial bugs," which I understood to mean, "Someone who's written Hello World and the To-Do List program."

I believe this subject can be taught to newcomers learning JS basics but the vocabulary here isn't written with beginners in mind but for people experienced in object-oriented class-based languages. It would make more sense if this were titled "Basic Knowledge of JavaScript for Intermediate to Advanced Java/C# Programmers: Access Modifiers and Inheritance" or something.
I'm not a programmer by education or training or anything so I don't know how much weight anyone should give my ramblings.

Don't hack vocabulary from other languages into JavaScipt (JS). Protected, private, public, final, etc.; toss them all. Those keywords and the phrase "access modifier" have no meaning in JS and shouldn't be used except when the audience has a background in languages that do. A beginner cannot web search for "JavaScript access modifiers" or visit the JS documentation at Mozilla Developer Network to learn more.

Perhaps the intended reader is experienced with other languages but I didn't get that impression from the line "So you're assumed to have at least a basic knowledge on writing Javascript and have written at least several easy, simple and small js files which work without nontrivial bugs," which I understood to mean, "Someone who's written Hello World and the To-Do List program."

I believe this subject can be taught to newcomers learning JS basics but the vocabulary here isn't written with beginners in mind but for people experienced in object-oriented class-based languages. It would make more sense if this were titled "Basic Knowledge of JavaScript for Intermediate to Advanced Java/C# Programmers: Access Modifiers and Inheritance" or something.
About the access modifier thing, that's why I've included these:
Please note that the following concepts will be used:
1. Final - Functions/variables that can't be redefined after their initial definitions
2. Private - Functions/variables only accessible by the enclosed class/instance/function
3. Protected - Functions/variables only accessible by the enclosed class/instance and their subclasses/instances
4. Public - Functions/variables accessible from anywhere
5. Static - Functions/variables shared by all instances of the same class
While access modifiers aren't built-in Javascript features at all, it turns out that many Javascript programmers don't want anything to be accessible from anywhere, so some of them are trying hard to emulate some sort of privacy in Javascript. Therefore I think these keywords, when explained to Javascript programmers having faced these issues, shouldn't be too alien to them, even when all they know is Javascript.

About the basic knowledge thing, I think having a basic knowledge is much, much more than Hello World or To-Do List. What I'm thinking about is something like the "Javascript Basics Tutorial" section in the "Javascript Tutorial" in TutortialsPoint, and I believe that site is at least slightly famous among Javascript programmers, as searching "Javascript Tutorial" in Google can easily find that site quickly.

Actually, I'm not really sure, as I've roughly 1 year of C++, 1.5 years of Java, and 2 years of Ruby experience before learning Javascript, and I personally find it really hard to go straight to the points without some sort of borrowing some concepts from some other undoubtedly popular languages.

Of course, I think I get what you mean. Most Javascript beginners not knowing any other programming languages, and not needing/wanting some sort of protections from having anything accessible from anywhere, will probably never be able to find such topics written this way, no matter how many sites have them. But I still think that as soon as they need/want such constructs, they'll ask for external helps and will be quickly introduced by the access modifier concept. Once they have that concept, they can then search for how to emulate them in Javascript.
I blame myself for not expressing my thoughts well. I'm not saying that JS programmers don't care for or have no use for privacy.

I'm saying define the intended audience. It seems this text is by DoubleX for DoubleX or other Java, C++, etc. programmers learning JS. I'm saying it's a disservice to write that the topic is appropriate for people who have only read the "Javascript Basics Tutorial" which doesn't include the concepts of inheritance and context A.K.A. this.

I'm saying link to relevant documents for JS. When JS inheritance is the context link to a JS specific article not some Wikipedia page that mentions the word prototype twice and JS never. That path is a dead end for a JS learner.

Use the words JS programmers use for the same reason. Closure is the name for the access modifier concept in JS. An abundance of articles are available on the JS closure topic whereas JS access modifier results are few.

I believe the concepts could be taught to a beginner but that adding all this extra vocabulary mutates the ideas from simple and maybe fun to complex and dead boring.
In the rule of K.I.S.S. I only write linear javascript and I don't know what any of that stuff is tbh.

See. I made a fun game with about 50 lines. Javascript Sokoban

I wish I didn't have to tinker with that other stuff till something happens.
I've added these parts to the 1st post:

At the very top -

Disclaimer: This topic's to provide some extra choices for those needing/wanting some protections from being able to access anything from anywhere while still allowing inheritance. As sometimes it completely makes sense for keeping everything public, these choices are entirely optional. Using any of them without any solid reason's just violating KISS for nothing, which should be avoided.



Right before really getting started -

Warmup
Spoiler
Recall that the only scoping instrument in Javascript is function, which goes hand in hand with closures. They're the very basis of emulating access modifiers in Javascript.
Without functions, anything's accessible from anywhere in Javascript, meaning that everything's public.

On the other hand, using the function scope lets one declare variables inside a function, making them only accessible within that function. These variables are thus local to that function, making them private.

When it comes to emulating protected in Javascript, one must first bear in mind that protected only makes sense when there's inheritance, which nearly always involve this.
Inheritance in Javascript are prototype based, which is different from almost(if not just) all the other programming languages supporting inheritance.
Nevertheless, protected in prototypical inheritance can still mean variables only accessible by the prototype defining them and all the other prototypes having that prototype as their parent.



Right after Situation -

You're highly encouraged and recommended to read the simplest thing that could possibly work first, which is demonstrated by lockableContainerKiss.js.
I love it instantly. Thank you DoubleX!
Currently I am using some ES6 features (which I didn't see you mention but I didn't read through all you code) to maintain a Final (protected?) class and to keep some variables private:
Code:
const FinalClasses = {};

Object.defineProperty(FinalClasses, "testClass", {
    value: function () {
        "use strict";
        var privateValue = '';

        Object.defineProperty(FinalClasses.testClass, "addToPrivate", {
            value: function (char) {
                privateValue += char;
                return true;
            },
            writable: false,
            enumerable: false,
            configurable: false
        });

        this.initialize.apply(this, arguments);
    },
    writable: false,
    enumerable: false,
    configurable: false
});
*note I do know that when using defineProperty that the writable, enumerable, configurable properties default to false.

Pros:
Entire class is non changeable.
Object class is stored in can not be reassigned/re-declared.
Is easy to use.
allows for private variables.

Cons:
ES6 features meaning it may not work on older browsers.
Can be verbose.

I am not certain how the above method compares against some of the methods you have shown.
I've made the following changes:
1. Changed the name of the 2nd pattern from Private Function to Composable Revealing Module.
This change leads to a better indication that the 2nd pattern's a special case of the revealing module pattern.
2. Changed the name of the 3rd pattern from Protected Class to Parasitic Inheritance, which supports multiple inheritance.
I feel very sorry for not realizing that I've just reinvented the wheel.

Currently I am using some ES6 features (which I didn't see you mention but I didn't read through all you code) to maintain a Final (protected?) class and to keep some variables private:
Code:
const FinalClasses = {};

Object.defineProperty(FinalClasses, "testClass", {
    value: function () {
        "use strict";
        var privateValue = '';

        Object.defineProperty(FinalClasses.testClass, "addToPrivate", {
            value: function (char) {
                privateValue += char;
                return true;
            },
            writable: false,
            enumerable: false,
            configurable: false
        });

        this.initialize.apply(this, arguments);
    },
    writable: false,
    enumerable: false,
    configurable: false
});
*note I do know that when using defineProperty that the writable, enumerable, configurable properties default to false.

Pros:
Entire class is non changeable.
Object class is stored in can not be reassigned/re-declared.
Is easy to use.
allows for private variables.

Cons:
ES6 features meaning it may not work on older browsers.
Can be verbose.

I am not certain how the above method compares against some of the methods you have shown.
I didn't mention any ES6 feature here as I haven't written any ES6 code, RMMV uses ES5 and it seems to me that ES6 still isn't widely adopted yet..
As for your method, I suppose it's meant to be extended by composition over inheritance then.
To compare with any of mine, I'll have to benchmark yours both in terms of memory and time consumption for object creation codes first 🙂
While it is true that RMMV uses ES5 and ES6 is still fairly young (from around 2015 I think?), ES6 does have some very useful features.
I am still not familiar with all the different programming terms, so thanks for linking to that page. 🙂
@DoubleX

I get that you seek protection in your javascript, but that stuff is client side dude.

All I have to do is open the web console and find a place to inject into your massiveSecretiveFunction something like

window.secretThis = this;

and now I can access all those secret properties you wrapped in 1,000 lines.

Your best bet is keep it simple (always remember KISS) and if you want secret stuff, use a real secret script like PHP and get your secret out of a database or something and name it from your PHP MD5 hashes or what not and JSON it into your script that way like Google recaptcha does for instance.
@DoubleX

I get that you seek protection in your javascript, but that stuff is client side dude.

All I have to do is open the web console and find a place to inject into your massiveSecretiveFunction something like

window.secretThis = this;

and now I can access all those secret properties you wrapped in 1,000 lines.

Your best bet is keep it simple (always remember KISS) and if you want secret stuff, use a real secret script like PHP and get your secret out of a database or something and name it from your PHP MD5 hashes or what not and JSON it into your script that way like Google recaptcha does for instance.
I would like to know how to use that window.secretThis = this technique to get all the strict private instance variables(for the already created instances as well as those going to be created), like those in lockableContainerKiss.js(i.e., KEY, MAX_KEY_MISMATCH_COUNT, IS_RESETTABLE, IS_RESETTING):
Spoiler
Code:
function LockableContainerKiss(KEY, MAX_KEY_MISMATCH_COUNT, IS_RESETTABLE,
        IS_RESETTING) {

    'use strict';

    var _MSG_INCORRECT_KEY = "The key doesn't match the lock of this container";
    var _MSG_MAX_COUNT_REACHED =
            'The maximum amount of tolerable key mismatches are reached';
    var _MSG_NO_CONTENTS = "This container has no contents";
    var _NULL_CONTENTS = {};

    var _contents = _NULL_CONTENTS, _isLocked = false, _keyMismatchCount = 0;

    function isLocked() { return _isLocked; };

    function keyMismatchCount() { return _keyMismatchCount; };

    function tryPutContents(contents, errback) {
        if (isLocked()) return errback('This container is locked');
        if (_contents === _NULL_CONTENTS) return _contents = contents;
        errback("This container already has contents");
    };

    function tryTakeContents(callback, errback) {
        if (isLocked()) return errback('This container is locked');
        if (_contents === _NULL_CONTENTS) return errback(_MSG_NO_CONTENTS);
        callback(_contents);
        _contents = _NULL_CONTENTS;
    };

    function tryUnlock(key, errback) {
        if (keyMismatchCount() >= MAX_KEY_MISMATCH_COUNT) {
            return errback(_MSG_MAX_COUNT_REACHED);
        }
        key === KEY ? _isLocked = false : errback(_MSG_INCORRECT_KEY);
        if (IS_RESETTING && !isLocked()) return _keyMismatchCount = 0;
        if (!isLocked()) return;
        _keyMismatchCount++;
        if (keyMismatchCount() < MAX_KEY_MISMATCH_COUNT) return;
        errback(_MSG_MAX_COUNT_REACHED);
    };

    var API = {
        'isLocked': isLocked,
        'keyMismatchCount': keyMismatchCount,
        'lock': function() { _isLocked = true; },
        'tryPutContents': tryPutContents,
        'tryTakeContents': tryTakeContents,
        'tryUnlock': tryUnlock
    };

    if (IS_RESETTABLE) {

        function tryResetKeyMismatchCount(errback) {
            if (isLocked()) return errback('This container is locked');
            _keyMismatchCount = 0;
        };

        API['tryResetKeyMismatchCount'] = tryResetKeyMismatchCount;
    }

    return API;

};

Also, in the case of Wrapped Prototype, those private static functions/variables aren't just for privacy, but also for memory and time performance when it comes to object creation codes by making the prototype smaller. I'll discuss that in solid understanding on using Javascript access modifiers and inheritance.

About KISS, that's why I've added lockableContainerKiss.js(just 64 lines) to this topic. Readers can simply compare this to the other versions and they can judge for themselves 🙂
Screen Shot 2017-02-26 at 7.30.45 AM.png
Screen Shot 2017-02-26 at 7.33.59 AM.png
Screen Shot 2017-02-26 at 7.34.15 AM.png
Screen Shot 2017-02-26 at 7.42.30 AM.png
I'd like to know the actual codes you've added 🙂

Original thread: https://forums.rpgmakerweb.com/threads/basic-knowledge-on-using-javascript-access-modifiers-and-inheritance.74763/