r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

14

u/TangerineX Apr 05 '22 edited Apr 05 '22

I think people take Javascript to be way more complicated than it needs to be. In javascript, there's really 4 things to think about

  1. "primitives", or various values. These are strings, numbers, and booleans

  2. Arrays, which are just an ordered list of things

  3. Objects, which is a map of key: value pairs, which are all primatives.

  4. Functions, which take some input, and turn it into something else

Basically EVERYTHING in javascript is built off of these concepts. If you understand these, you've understood 90% of javascript.

Surprisingly, I've learned more about Javascript by learning Typescript instead, which gives a more opinionated way of using Javascript in a way that makes a bit more sense. It's also become the standard for writing stuff in many frameworks, such as Angular and React.

1

u/__Fred Apr 06 '22

Can you explain the "prototype" property?

If I want to access a property on an object and JS can't find it, it looks inside the prototype, right? No... https://www.w3schools.com/js/js_object_prototypes.asp I used to understand this. It feels like the most complicated part of JS but you don't need it in TypeScript.

Okay... JS builds up OOP out of dictionaries and it needs "prototype" for that. Maybe it would be useful to see how one would go about doing that to Python?

2

u/TangerineX Apr 06 '22

prototypes is only a thing because people wanted OOP in Javascript. In the modern day, most people prefer using POJOs (plain old java objects) and not inherit behavior, only types definitions (such as implementing a interface).

Your source doesn't contradict what your initial understanding is. The prototype is really just an internal implementation of OOP where each Object has a built in prototype field, which acts sort of like a linked list to the next prototype. If you call a function within an object, and it doesn't have that function defined, it will check down the prototype chain until the final prototype hits null (usually at Object). Prototypes are why every object in javascript has a toString function even if you don't define it yourself.

Python does something similar in that it builds metadata of the classes which python objects inherit from, but it's a bit more complicated, as Python allows for multiple inheritance. I wouldn't recommend trying to write code based on inheritance in either language, as neither are built from the ground up to specifically support it. Python's class system results in pretty buggy code, and using classes and inheritence in Javascript usually results in much larger compiled JS binaries. This was one of the primary reason why React transitioned from class based components to functional components and hooks. Angular doesn't use inheritance for it's components either, it uses annotations instead.