Prototype inheritance in JavaScript

Authored by Owen Lamb and Katya-yani Vyas

JavaScript is a classless programming language. This is quite different from classy languages like ruby where classes can inherit from other classes and objects are created within classes. In javaScript inheritance is prototype based, there are no classes and objects inherit from other objects. Prototype is an internal javaScript object which allow other objects to inherit properties. In our thermostat application for example we created a variable Thermostat, which is a function, and we gave the object other properties through prototyping. Look at the code example below:

var Thermostat = function(){
this.currentTemperature = 20;
this.maximumTemperature = 25;
Thermostat.prototype.temperature = function() {
return this.currentTemperature;
}; //Our Thermostat object inherits Thermostat.temperature function
Thermostat.prototype.increase = function() {
if (this.currentTemperature < this.maximumTemperature){
return this.currentTemperature+=1;
return this.currentTemperature;
};//Our Thermostat object inherits Thermostat.increase function

These objects can be extended, reused, and modified to add more functionality as your application develops.