“Object-oriented programming is a programming pattern that uses self contained pieces of code to develop applications”

Object-oriented programming, also know as OOP, makes use of techniques such as modularity, inheritance, polymorphism and encapsulation. OOP means creating a collection of objects that perform specific tasks and can communicate with other objects. By encouraging modularity, OOP offers greater flexibility and maintainability in programming.

JS is a prototype-based programming language. It employs cloning and not inheritance. A prototype-based programming language is a style of object-oriented programming that uses functions as constructors for classes. Although Javascript has a keyword class, it has no class statement. This distinction is important when comparing JavaScript to other OOP languages. (Mozilla)

Terminology

Namespace
A container which lets developers bundle all functionality under a unique, application-specific name.

// Global namespace
var Animal = Animal || {};

// Sub namespace
Animal.info = {
  age: 30,
  weight: 75,
  sayHi: function() {
    console.log('hello');
  }
};

Class
Defines the object’s characteristics. A class is a template definition of an object’s properties and methods. JavaScript uses functions as constructors for classes.

// Class instance
var bonnie = new Animal();

Constructor
The constructor is called at the moment when an object instance is created.

var Animal = function() {
  console.log('object instance created');
}

var bonnie = new Animal();

Property
Variables which are within the class.

var Animal = function(name) {
  this.name = name;
}

var bonnie = new Animal('bonnie');

// bonnie
console.log(bonnie.name)

Methods
Basically functions – similar in nature to properties – which are called with opening and closing parenthesis.

var Animal = function(name) {
  this.name = name;
}

Animal.prototype.barkName = function() {
  console.log(this.name)
}

var bonnie = new Animal('bonnie');

// bonnie
bonnie.barkName();

Inheritance
Objects obtaining features from parent objects.

function Animal() {
  this.init = function(name) {
    this.name = name;
  };
}

Animal.prototype.move = function () {
    return true;
};

function Dog(name) {
  this.init(name);
}

Dog.prototype = new Animal();

Dog.prototype.bark = function () {
    return true;
};

function Cat(name) {
  this.init(name);
}

Cat.prototype = new Animal();

Cat.prototype.meow = function () {
    return true;
};

var bonnie = new Dog('bonnie', 12);
var minnie = new Cat('minnie', 5);

// true
console.log(bonnie.move());

// true
console.log(bonnie.bark());

// is not a function
console.log(bonnie.meow());

// true
console.log(minnie.meow());

Encapsulation
Including everything that’s needed into a class to ensure it can operate correctly.

Polymorphism
Uses inheritance to create objects that share behaviors but can also be overridden

 

Refs

Introduction to Object-Oriented JS – Mozilla

OOP in JS – Javascript is Sexy

Worthy of Note

Worthy of Note is a site aimed at Web Designers & Developers. It offers a wide range of resources to help assist anyone in the web industry.

View