24
u/not_a_gumby Aug 10 '21
Prototypes are basically Javascript's version of abstract classes.
Notice how every class can extend another super class? the prototype is that previous extension. You can follow the extension all the way up to the highest level of class.
14
u/Valoruz Aug 10 '21
Yeah I know what prototypes are, not until I read Eloquent JavaScript. I had started JavaScript with Codecademy and got nowhere. But after reading Eloquent JavaScript I am really feeling I know something...
6
3
u/TalonKAringham Aug 10 '21
Going through that book and actually doing all the exercises and projects will get you a loooong way.
4
u/Aoshi_ Aug 10 '21
Iâve seen ES recommended a lot. Iâve read a few chapters and done exercises but itâs so dense. I almost wish there was a YouTube or udemy course that goes through the book and makes it a bit easier for my tiny brain to understand.
Listening is easier than reading for me.
2
u/adenzerda Aug 10 '21
Prototypes are basically Javascript's version of abstract classes
Sort of? Except that by definition every object in js is concrete because everything is an instance at runtime
1
u/not_a_gumby Aug 10 '21
yeah, not literally. It's just the way of thinking of inheritance and descandants of classes. Abstract isn't exactly what they are.
3
u/RealNerdEthan Aug 11 '21
Glad I'm taking Colt Steele's Wed Dev Bootcamp on udemy then! He has a whole section on Prototypes, Classes, and Object Oriented Programming.
Link for anyone interested: https://www.udemy.com/course/the-web-developer-bootcamp/
1
u/Valoruz Aug 11 '21
I want to take that course too. But I am unable to pay. Does it go free frequently @RealNerdEthan ?
1
u/RealNerdEthan Aug 11 '21
I haven't seen it go free but it's $18.99 USD right now which is a heck of a deal for the amount on content you get.
It's 63hrs of lecture spread across over 680 lessons. There practice questions and projects, and also a great discord community when you need help.
I'm currently in section 32 and I've learned more in this course than any other by far because Colt goes into detail about what something is AND why you would use it. Then you work in creating something together in the lesson and afterward there's often a short challenge to complete. Finally it all builds to a final project where you create a Yelp clone and learn how to fully deploy it with backend technology such as NodeJS.
It's a lot of content but if you're serious about making a career in web dev or trying to learn your first programming language then I can't recommend this course enough đ
25
u/Me-A-Dandelion Aug 10 '21
I passed every level of Grasshopper but still can't write a proper thing đż
8
u/jellatin Aug 10 '21
You need to build some actual projects vs just learning syntax and reading tutorials.
Itâs much more effective to try to attempt to build a Twitter clone with real world problems and use cases that force you to look for specific answers/strategies in the language.
If you read all of a language textbook or course, even if you internalize it, youâre going to quickly feel lost the first time you start building something.
Itâs like learning to drive a car just by reading or watching youtube videos. The knowledge alone isnât enough, you need the muscle memory.
7
u/EIGRP_OH Aug 10 '21
Can someone tell me the precise difference between prototypes and classes?
11
u/sogekihei_7 Aug 10 '21
Classes are synthetic sugar for prototypes to make it easier to work in OOP paradigm.
13
9
u/shuckster Aug 10 '21
I believe one of the biggest hurdles of understanding classes vs. prototypes in JavaScript is the history of the word "class" and "object", and how the dominance of JavaScript has blurred the once clearly defined line between them.
In classical compiled languages, "class" quite literally means "blueprint": Instructions for what an "object" should look like once Random Access Memory has been allocated for it. Classes = offline, on disk. Object = online, in memory.
The allocation process -- the act of creating an object from a class -- is called "instantiation". This is a manual step done by the programmer.
object = new MyClass/MyClass objectfor example. No instantiation, no object.JavaScript has no concept of this. At all.
There are no "classes" from which to instantiate objects. We create new objects merely by declaring them, like
let object = {}. They will be allocated memory the moment the interpreter passes over them, while a class in a compiled-language will sit idle until deliberatelynew'd.Despite the modern-day compositional/functional push, JavaScript was created in a time when OOP concepts where en vogue, and classes where how you got things like inheritance and polymorphism. But what if you don't have classes? This is where the prototype system comes in.
A classical class, even one based on a chain of inheritance with supers and so on, will produce a single object in memory when instantiated. Unless explicitly written by a programmer, all instantiated objects "stand alone", and know nothing of any other in-memory objects.
But a JavaScript object belongs to a chain of in-memory objects, all linked to their parents via ancestral
prototypevariables. All chains lead back to the "God object",Object, from which everything inherits. A function/prop added to any object-prototype makes it immediately available on its descendants (those whoseprototypevariable point back to it), and that's how we get inheritance without classes.You might argue that prototypes do have a kind of "blueprint-y" feel to them, so at first glance the distinction between them and classes might seem like pedantry. But just like its host, a prototype is a live, in-memory object. A class isn't.
Incidentally, this might shed a little light on where the phrase "in JavaScript, everything is an object" comes from: It's nothing to do with whether or not primitives/functions are "real objects": Rather, it alludes to the fact that JavaScript is an interpreted/scripting language, not a compiled one. All the "classical" classes that power a JavaScript interpreter (written in C++ for example) will have already instantiated the objects tasked with the job of running your JavaScript code in the first place.
So a JavaScript "class" is much more conceptual than the rigid and defined classes of Java/C++. The
classkeyword is an API for easier digestion of the object-prototype system -- which itself offers a form of inheritance that more naturally fits an in-memory, dynamic language like JavaScript where "everything is an object" in the classical sense of the term.This isn't to say that one particular definition of "class/object" is the "right" one or not. Just hoping this sheds a little light on the (greatly simplified, please don't shout at me) history of the terms.
13
3
Aug 10 '21
I got destroyed in an interview the other day over proyotypes.... thought my freecodecamp course and a couple of projects made me good...
1
6
u/rakshitshah94 Aug 10 '21
I was so much obsessed with the word "Prototype" when I was in 10th Grade, whenever someone asks my username, I used to tell them "PROTOTYPE", hahaha... I am now working on prototype models as a software engineer (Full-stack developer) for the last 5 years in the IT industry.
2
Aug 10 '21
They finally built a "workspace" for practicing code without having to try to figure out Atom and GitHub. I'm restarting all my coding courses on there.
2
u/jsAlgo Aug 10 '21
Just wanted to add one resource website here.
Javascript.info
The only website that cleared my doubts about prototypes. Short and upto to the point.
3
1
u/Gantzz25 Aug 10 '21
So prototypes in JS are like abstract classes in Java for example?
4
u/shuckster Aug 10 '21
[1, 2, 3, 4].notReally() // Uncaught TypeError: [].notReally is not a function Array.prototype.notReally = function () { const alrighty = 'if you say so'.split(' ') return this.map((_, i) => alrighty[i % alrighty.length]) } [1, 2, 3, 4].notReally() // ["if", "you", "say", "so"]
1
u/franker Aug 10 '21
FYI - went through the book several years ago, but the Head First Javascript Programming book has good treatment of prototypes.
1
u/forresthopkinsa Aug 11 '21
Unpopular opinion: if you don't understand prototypes then you can't call yourself proficient in JS.
Same goes for scope, closures, promises, async/await, generators, etc.
If this describes you, find the book You Don't Know JS online. It's free, read it.
Your mastery of a language is only as strong as its weakest link.
0
u/Lunacy999 Aug 10 '21
Itâs not just JavaScript to honest. The reason behind (my take) on why JS has such notoriety among developers is because of its extensive use as a scripting language before frameworks and libraries such as jQuery, Angular, React etc came in. Another big reason is that JS is a dynamically typed language. Unlike languages such as Java, C++ where type checking is done at compile time, JS does this at run time. And this throws off a lot of new developers who come into this language. And with the use of libraries and framework such as React, a lot of the internal workings of JS is abstracted away, especially in those tutorials where they preach âthe only JS you need to know for xxâ. JS as a language is fairly straightforward, provides you experiment with Native JS and not at the expense of a library or framework. Recommend reading the you donât know JS series.
0
Aug 10 '21
[deleted]
27
u/MarcoJHB Aug 10 '21
100% this. Did all the HTML/CSS/JS courses on there. Took weeks, and I felt like I was becoming a programming guru. Like a week later I tried to build a simple page and couldn't even center a div.
10
u/al_balone Aug 10 '21
Iâm finding this. Ideally Iâd like about 20 exercises all based on each concept, even if it just means muscle memory helps eliminate some of that head scratching you end up doing when you canât remember how to even write your first line of code.
15
u/MarcoJHB Aug 10 '21
Yup. Wish I knew this earlier, but now my favourite obsession is sites that don't hold your hand. Frontendmentor, codewars, etc. Yes, I'll probably take a week to build something small, but I did it all on my own. Yes, you can look at the answers to see how other people did it, which helps with refactoring, but it really helps to stretch your muscle memory.
It's also way more of a confidence booster. "hey - I made this login form all on my own!" vs "Hey - I just followed this 4hr tutorial to build a Netflix clone in react, I don't remember most it but it's cool right?"
11
u/al_balone Aug 10 '21
Haha yeah, I did some really basic array thing on codewars and was so pleased with myself I emailed my wife the code.
4
9
u/not_a_gumby Aug 10 '21
Yeah, its not great honestly. I tried it fora few weeks early on, got nowhere, and just started spamming Udemy courses that were all 100% project focused.
Turns out I learned way faster by just being thrown in the deep end.
1
u/Aoshi_ Aug 10 '21
Any recommendations? Iâve been doing freecodecamp and have built some smaller things but I want to do more projects.
3
u/not_a_gumby Aug 10 '21
What stack do you want to learn? For Javascript/Node stacks, focus on Node/Express for backend and React for frontend. That's the way I went.
I have a rather long blog post here with links to all of the courses that I used to get myself schooled up on Javascript and React! Skip to the section "Just Tinkering" for where some of the courses section begins.
3
u/GoOsTT Aug 10 '21
Yeah, did the codecademy full stack course and felt like a fucking OG programmer to be dissapointed in the first project of my own.... Took me 9 months to do the course, another 9 to actually be able to program by my self.... This isn't mentioning algo and data.
4
u/samuelcbird Aug 10 '21
When the pandemic hit they gave Premium to some people for free. You just had to prove you were retraining and passionate and voila.
I was one of those people and I went through a few courses, focusing mainly on their React course. It wasnât until afterward and I started making a project of my own and reading some documentation that I realised the course was completely out of date. It was very specific and seemed to miss a lot of information.
So even after doing a premium course I felt completely unprepared in the wild. I learnt more from just doing a project of my own and reading documentation and various informative articles on the internet.
I donât really like Codecademyâs teaching style and I wouldnât recommend anyone pay for it.
2
2
-1
u/Blacknsilver1 Aug 10 '21 edited Sep 05 '24
smart books outgoing crush poor fuel bewildered public ancient steep
This post was mass deleted and anonymized with Redact
165
u/BowlingForPriorities Aug 10 '21 edited Aug 15 '21
Great, now I have to Google this for my senior interview tomorrow đ
Edit: Itâs how JavaScript handles inheritance. Thanks OP
2nd edit: I passed the first interview, have an interview with the squad lead next week