JS: Constructor and Class
Prototype Based Object System (Object Based Inheritance)
- JavaScript is a prototype based object system. (Not class based as in Java and Python)
- JavaScript prototype system means, every object can have a parent object, and when property is accessed, it checks the parent object to look for the property.
- You can specify the parent object when you create a object.
- You can also change the parent object anytime.
- This is a very simple system, and is nice in JavaScript.
- For detail about how this works, see JS: Prototype and Inheritance
History of JavaScript Constructors and Class Complexity
- JavaScript is originally named Mocha, and then LiveScript.
- Designed in 1995. It is meant to be a easy scripting language for non-programers who are writing HTML, to make it possible to have interactive dynamic HTML.
- JavaScript tried to look and feel like Java language's class based object system.
- This is done for social marketing reasons back in 1995.
- Java was new programing language, as a competitor to the C++ programing language, and it is heavily advertised.
- Sun Microsystems, the creator of Java, made a deal with Netscape, to have Java bundled as part of the Netscape browser.
- For this reason, LiveScript is renamed to JavaScript, and one goal is to have it look like Java.
- This forcing JavaScript to be like java, is technically a very bad thing from a programing language design point of view.
- You are basically forcing 2 incompatible systems to be similar.
- A prototype based object system is incompatible with class based object system.
- This results in a lot complexity in JavaScript.
- What made it worse, is that the design is done badly and in a rush.
- It is said, the whole design and implementation of JavaScript is done in 10 days. (by Brendan Eich)
Unnecessary and Bad Complexity in JavaScript
- Unnecessary and confusing Operator
new
- Unnecessary and unpredictable value in Keyword
this
- Unreliable Operator
instanceof
- Magical prototype key
prototype
- Magical prototype key
constructor
and is unreliable.
2015 Addition of the Keyword Class
- In 2015, JavaScript tried to have a overhaul of design, yet remain backward compatible, so they added a new
class
keyword. - This did not help, because, one can create object and parent easily and simply without using any of the complex
new
andthis
. - But most industry programers are used to
new
andthis
, thinking it's similar to other popular language's class based system such as Java, Python. - So the JavaScript committee decided to add
class
keyword, yet still JavaScript object system has nothing to do with Java's Class System, nor Csharp, Python, Ruby, PHP.
This chapter, is detailed explanation of these.
JavaScript, Constructor, Class
- JS: Constructor and Class
- JS: this (binding)
- JS: Constructor
- JS: prototype (property)
- JS: new (operator)
- JS: instanceof (operator)
- JS: constructor (property)
- JS: typeof, instanceof, .constructor
- JS: class (keyword)
- JS: Class Expression
- JS: typeof Class
- JS: static (keyword)
- JS: extends (keyword)
- JS: super (keyword)
- JS: Define a Class Without class