Author Topic: Basic knowledge on using Javascript ES5 access modifiers and inheritance  (Read 1178 times)

Offline DoubleX

  • Trained Member
  • *
  • Posts: 19
  • LV: 4
  • Gender: Male
  • Just a nameless weakling
    • View Profile
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
(click to show/hide)

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

Situation
(click to show/hide)

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
(click to show/hide)

Composable Revealing Module
(click to show/hide)

Parasitic Inheritance
(click to show/hide)

Reversed Inheritance Hierarchy
(click to show/hide)

Reversed Prototype Chain
(click to show/hide)

Summary
(click to show/hide)

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 :)
« Last Edit: March 04, 2017, 04:20:08 AM by DoubleX »

Offline DoubleX

  • Trained Member
  • *
  • Posts: 19
  • LV: 4
  • Gender: Male
  • Just a nameless weakling
    • View Profile
Re: Basic knowledge on using Javascript access modifiers and inheritance
« Reply #1 on: February 15, 2017, 04:50:48 PM »
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.

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19954
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Basic knowledge on using Javascript access modifiers and inheritance
« Reply #2 on: February 15, 2017, 04:55:47 PM »
Would you be interested in putting together a post with all currently existing design patterns in general (or at least the major and mostly used ones)? I'm sure this would be a huge help for many people.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

Offline DoubleX

  • Trained Member
  • *
  • Posts: 19
  • LV: 4
  • Gender: Male
  • Just a nameless weakling
    • View Profile
Re: Basic knowledge on using Javascript access modifiers and inheritance
« Reply #3 on: February 15, 2017, 05:22:22 PM »
Would you be interested in putting together a post with all currently existing design patterns in general (or at least the major and mostly used ones)? I'm sure this would be a huge help for many people.
As there are at least nearly a hundred of them, and AFAIK the Javascript community is actually very diverse(i.e., their preferences, problems to solve and working environments vary greatly), I think such attempts would demand a shocking amount of hard work.

Actually, I think the first 3 pattern I've covered might be major and mostly used ones:
The wrapped prototype is used by the RMMV plugin community all the time, as the default RMMV codebase uses prototypes and immediately invoked function expressions are used to wrap the plugin contents.
The revealing module pattern is still popular today, at least when it comes to writing reusable, application specific and/or page specific client side scripts.
The parasitic inheritance is also popular, and it seems to me, from what I've found on the internet, that some leading IT companies uses parasitic inheritance heavily.

The 4th and 5th are probably alien to the entire Javascript, or even the whole programming, community, as those patterns would likely be regarded as tools exclusively for the control freaks by the majority, and reversing the control of the inheritance hierarchy and/or the prototype chain is extremely counterintuitive, and incredibly dangerous without realizing what's really going on.