Search code examples
javascriptjavaclassoopprototypal-inheritance

Why are JavaScript classes not considered "real" classes? (compared to Python, Java, etc.)


Why are JavaScript classes considered not "real" compared to those in a "standard" class-based languages such as Java? I know that JavaScript classes use prototypes under the hood, but doesn't Java code compile down to getfield,invokevirtual etc. that perform dynamic lookups as well? Other than the fact that prototypes are regular objects that can be modified arbitrarily at run time and that you can't implement access control or multiple inheritance with prototypes, how are Java' classes fundamentally different from JavaScript's?


Solution

  • Because JavaScript doesn't have classes; it's a prototypE-based OOPL.

    The syntactic sugar of ES6 classes adds no additional functionality to ES5. Classes are implemented on top of its native OOP paradigm: prototypal inheritance can mimic classical inheritance, the reverse is not true (with caveats).

    You may actually be asking a duplicate of this question, with the (yet another) caveat that points are applicable to varying degrees depending on which specific languages are being discussed (e.g., Ruby's classes and instances are a lot different than Java's.)

    Access control and multiple inheritance are orthogonal discussions, e.g., Self has (prioritized) multiple inheritance, you could implement access control in Lua via metatables/metafunctions, etc.