ES6 introduces the class keyword into the language. It does not, however, introduce a new model of object-oriented inheritance. The class keyword provides syntactic sugar for expressing prototype properties and constructor functions as described in this article.
There are few language features that in concert make inheritance work.
Calling new Car() creates a new object with prototype set to the value of Car.prototype. This allows setting up prototype chain for new objects. Note that new Car() only makes sense when Car is a function. Such function is also known as a constructor function.
Calling a function property of an object, the value of this is bound to the containing object. For example "abc".toString() calls the toString function with this value set to "abc". This allows code reuse - executing the same code lines with different value of this. A function property of an object is also called a method.
Let's create an example that calculates the perimeter of a rectangle with object-oriented programming.
First a constructor function Rectangle is defined. The convention is to capitalize function names that are to be called with new to separate them from other functions. The constructor function is initially given an empty object as the value of this and the code populates it with x and y properties for later use.
Then, a function property is added to Rectangle.prototype that calculates the perimeter using the object's x and y properties. Notice the use of this that allows the same code to work on multiple objects with different values for the variables.
Finally, a new Rectangle object called rect is created. It inherits from Rectangle.prototype and allows us to call rect.perimeter() and print the results to the console.
Misleading prototype property name
The value holding the prototype of an object is sometimes called the internal prototype link. It's also been historically called __proto__, a name that has some controversy. To be politically correct, it can be called the value that's returned from Object.getPrototypeOf(...).
File uploads get saved with 0 bytes?
You know how you can spend hours reading a file upload tutorial that doesn't leave you fully off on your own - files still get created with 0 bytes in your project. Learn how to get files, safe and sound, uploaded on a server.