Basic knowledge on using Javascript ES5 access modifiers and inheritance
#1
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
Content Hidden

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

Situation
Content Hidden

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
Content Hidden

[u][i][b]Composable Revealing Module[/b][/i][/u]
Content Hidden

[u][i][b]Parasitic Inheritance[/b][/i][/u]
Content Hidden

Reversed Inheritance Hierarchy
Content Hidden

Reversed Prototype Chain
Content Hidden

Summary
Content Hidden

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 [Image: clear.png]:)
My RMVXA/RMMV/RMMZ scripts/plugins:
http://rpgmaker.net/users/DoubleX/scripts/
Reply }
#2
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.
My RMVXA/RMMV/RMMZ scripts/plugins:
http://rpgmaker.net/users/DoubleX/scripts/
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Basic knowledge to the default RMMZ TPBS battle flow implementations DoubleX 0 1,654 03-05-2022, 07:33 AM
Last Post: DoubleX
   Basic knowledge to the default RMMZ turn based battle flow implementations DoubleX 0 1,635 03-01-2022, 05:11 AM
Last Post: DoubleX
   [RMMZ]Using ES6 Class Inheritance/Extensions For Aliasing Without Prototypes DoubleX 0 2,732 08-01-2020, 03:19 PM
Last Post: DoubleX
   Naive ideas on basic 2D collision detection with simple shapes DoubleX 0 3,800 07-05-2016, 02:28 PM
Last Post: DoubleX
   Basic knowledge in concurrency with a centralized clock DoubleX 0 3,520 07-05-2016, 02:22 PM
Last Post: DoubleX
   The Basic, Still-Image Battler Tutorial Ratty524 0 4,679 12-06-2008, 04:31 PM
Last Post: Ratty524



Users browsing this thread: 2 Guest(s)