How do
- prototypes
- objects
- constructors
- “this”
- functions
work in JS in relation to each other (and what exactly are they; is a constructor a function, object, prototype, “this”)?
Can someone please clarify this? I have an idea of what each of them are and how they work, but not a clear one.
I think it would be easier to understand questions that arise, like, for example: “Is a.constructor the same as a.prototype.constructor“, if one knows what these things are.
Object – A collection of name-value pairs, for example:
Constructor – a function that ‘creates’ an object, for example:
Prototype – a special type of object, from which other objects inherit properties. Every object has a prototype. You can use them to add a method to all instances of an object, for example:
Now that you have defined a
doSomethingmethod on theStringprototype, allStringobjects can use it:For more information about the JavaScript language and how it works, I suggest you take a look at the ECMAScript spec, or for something a bit lighter, read this.