"Best Practices" for creating JS Classes/Prototype functions?

"Best Practices" for creating JS Classes/Prototype functions?
0.0 0

#1

I see so many different ways to do this, even within the beta freeCodeCamp?

I know we haven’t gone over ES6 classes, so that’s already one way. But as far as defining Object Functions that act like classes (Pre-ES6) it seems there are so many different ways!

Sometimes it’s definining the methods within a new function object (named or unnamed), sometimes it’s making a new object and setting it to a variable and then setting properties such as Dothis:function(){whatever}. Sometimes it’s making the prototype functions on the outside of the function delcaration such as Whatever.prototype.doThat = function(){stuff;}

Im the kind of person that likes to figure out the best way and stick with one or two ways. Thats the confusing part of JS…there are SO many different ways to do one thing. How do you figure out whats best?

Thanks!


#2

Good question…
I have most commonly seen prototypes (pre-ES6) defined as:

function Foo(name) {
	this.name = name;
}

Foo.prototype.myName = function() {
	return this.name;
};

I took this from the “this & Object Prototypes” chapter of You Don’t Know JS.


#3

I need to read those books, I see them referenced a lot. I didn’t know if they’d give much after going through FCC. Can’t hurt though.