We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
Thanks for the great feedback! I'm really happy it helped you 🙂 keep on learning and coding 👏💪
This by far the best explanation of this topic i have seen online. can i ask, what javascript books you recommend a beginner like to me read?
Thank you! 🙂
I don't read books much so can't recommend on any. Sorry
Without a doubt the best literature I've read on this subject in my 8 year journey with "this" and "prototypal inheritance", thanks for writing this up!
Quick feedback:
I believe the following// link paidPlayerFunctions object to createPlayer object
should be// link paidPlayerFunctions object to playerFunctions object
Threw me off a little.
Quick question on another topic I have to refresh on every year(hoisting):
Here:
const playerFunctions = {
setScore(newScore) {
this.score = newScore;
}
}
function createPlayer(userName, score) {
const newPlayer = Object.create(playerFunctions);
newPlayer.userName = userName;
newPlayer.score = score;
return newPlayer;
}
How's createPlayer able to read playerFunctions, given that it's initialized first?
Thank you for the great feedback and correction! ❤️
I'm not sure i understand your question, are you asking how is createPlayer able to use playerFunctions because createPlayer is a function, thus hoisted "above" playerFunctions?
If so, then this is not a matter of hoisting but invocation-time. I mean the engine doesn't really execute the code inside the body of createPlayer until we actually call / invoke it.
i.e: createPlayer()
by that time, playerFunctions is already initialised.
Make sense?
Ah, yes you're right, had a brief dumb moment. Thanks again sag1v , super helpful!
I'm reading the same article as ligiacurupana said below and found this article really useful. The code breakdown/walkthrough examples are brilliant. Thanks for taking the time to post this up.
What a great article... Thank you!
PS: One question I was left with in the end is whether the different ways can be combined. Imagine I create a function-generated class because I am not expecting any inheritance so it is not too much effort, but then requirements change and I decide to subclass it with the 'class' keyword to avoid the effort going to prototype chaining. I am guessing that should be fine? Maybe adding a note on this could be helpful
Thanks!
As for your question, can you share a small snippet to illustrate the use case?
//Creating the parent class in function-generated style...
function Parent(a,b) {
this.a = 1;
this.b = 2;
}
//add a setter
Parent.prototype.setC = function (c) {
this.c = c;
}
//check class
const parent_obj = new Parent(1,2);
console.log(parent_obj) //Does not print c
parent_obj.setC(3) //Initialize c
console.log(parent_obj) //Prints c too
//Now child formation using 'class'-like style
class Child extends Parent {
constructor(a,b,d) {
super(a,b);
this.d = d;
}
//add a setter
setE(e) {
this.e = e;
}
}
//check child class
const child_obj = new Child(1,2,4);
console.log(child_obj) //does not print e
child_obj.setE(5) //set e
console.log(child_obj) //prints e too
So, to my understanding it works just fine. The whole idea is to switch from function-generated creation to 'class-keyword' creation once inheritance comes to play (and manual chaining becomes a pain).
I have no idea if this actually has a use case in real life. It just came up to me as a question...
Being a beginner in Javascript (hence not having had read much code) I wonder what the best practices of classes are and whether it is truly dangerous to just go with the more convenient 'class-keyword' declaration...
PS: I couldn't manage to indent
Wow man, I can't thank you enough. I'm reading this article to understand the "setState error" on React. I'm finally understanding things clearly as a whole. This article, the article about the "this" keyword, and the other on the "setstate error" on React are building for me a solid understanding about the JS language. This will make a huge difference in my life as a developer.
It took me a few hours to understand things clearly and finish reading this article. I had to write the code on my pc to make sure I really got the important details on the topic.