This is a question about JavaScript Prototype Chaining.
I've read some documentation about this, and the syntax doesn't really make much sense to me.
https://wildlyinaccurate.com/understanding-javascript-inheritance-and-the-prototype-chain/
The way I've understood JavaScript Prototypes is essentially some syntactic candy that wraps around a function call which just adds properties to a new object named this.
Basically, turning what you might do in JavaScript without prototypes from this:
function PsuedoClass(arg0, arg1){
var this = {};
this.arg0 = arg0;
this.arg1 = arg1;
return this;
}
var obj = PsuedoClass(0, 1);
into what we all know and love:
function PsuedoClass(arg0, arg1){
this.arg0 = arg0;
this.arg1 = arg1;
}
var obj = new PsuedoClass(0, 1);
So naturally, the way one might expect to do inheritance in JavaScript would essentially to be call the constructor functions in a chain. It only makes sense, Apply all the properties from both PsuedoClasses into one object.
The whole obj.prototype thing just seems out of the blue to me. It seems to destroy the natural flow that JavaScript seems to lead you on. It would be much more intuitive if just using PsuedoClass.call(this, arg0, arg1); would be enough.
And it almost does, except then javascript's instanceof operator fails.
And there's more arguments than just increased readability that JavaScript's obj.prototype should be removed.
It simplifies the JavaScript Inheritance, reducing things from 2 lines to 1.
If they'd make PsuedoClass.call(this, arg0, arg1) cause this to be an instanceof PsuedoClass, it'd make it possible to do things like this:
function CustomRowClass(){
this.someCustomMethod = function(){}
}
var tableRow = document.getElementById('myRow');
CustomRowClass.call(tableRow);
console.log(tableRow instanceof CustomRowClass); //returning true
tableRow.someCustomMethod();
I'm really not trying to bash JavaScript as a whole here, I think the language does make a lot of sense in many ways, its just this Prototype chaining is not what I expected it to be, and it doesn't really make intuitive sense.
Am I missing something here, is there a way to understand this obj.prototype in an intuitive way? Everything about it just throws things off for me.
Or is there a simple way to use PsuedoClass.call(this, arg0, arg1); and get the desired instanceof behavior?