Chris J. Lee

Dallas Drupal Developer

You are here

What does it mean to be a prototypical language in terms of Javascript?

Prototypical Inheritance, means that an object inherits means it inherits directly from the object.

Or in other words, an object exists as a template for other objects.

JavaScript is a prototypical language. In each and every object, there exists a hidden prototype method. One can create a prototype method to the base class and each of the child class will inherit these methods:

var Car = function(color, make, mileage) {
  this.color = color || "Blue";
  this.make = make || "unknown" || null;
  this.mileage = mileage || 0;

var Cadillac = function () {
  this.convertible = true;

Cadillac.prototype = new Car('Black', "Cadillac");  // Define default constructor

var myCar = new Car(),
myCaddy = new Cadillac();
console.log(myCaddy .color); // Black
console.log(myCaddy .make); // Cadillac

console.log(myCar.convertible, '. It is convertible.'); // true

Child objects and Prototypes

Upon adding a prototype, child functions will initially inherit the original Abstract classes value if it's specified:

// After defining the prototype. I can add methods to the child objects after they've been created.
Car.prototype.honk = function (sound) {
   if (window.console) console.log(sound || 'HONK!');

myCar.honk(); // logs to console, "HONK!"


© 2017 Chris J. Lee