I bet this has been explained somwhere else, but I do not know how to search for it. I have read many explanations and approaches to OO in JS without something like TS or new ES6 and everywhere, a different syntax is used, but never compared all those approaches together.
As far as I know, there are 3 ways to create a new object in JS as we know it form different languages out there. But what are the differences and what is best practice to achieve this OO design?
Approach 1:
var Foo1 = function() { this.a = 1 }
var testFoo1 = new Foo1();
console.log(testFoo1.a); //prints "1"
Approach 2:
function Foo2() { this.a = 2 }
var testFoo2 = new Foo2();
console.log(testFoo2.a); //prints "2"
So far so good, the code is pretty much the same, except the syntax for the declaration of our object function. But already now, I am confused. What are differences here? Is it really just preference?
Last approach is the prototype chain, which needs more code or at least not that well structured code in my opinion, because you can not write it inbetween one bracket.
Approach 3:
Foo3 = function() { }
Foo3.prototype.a = 3;
testFoo3 = new Foo3();
console.log(testFoo3.a); //prints "3"
I hope my question and examples are right and it all makes sense and is not redundant. If it is so, please leave me a comment and I will correct or delete this entry, but do not downvote without writing why. Thank you so much.
EDIT I wanted to point out the object creation process and differences and best practice in plain JS with this question.