This is the third article of the series of Object Oriented JavaScript. In previous article we have seen private, public and static types of method.
Constructor
Lets start this article with Constructor as whenever you instantiate a class, you need to initialize some default values for this we initialize values in constructor but as in JavaScript Class is nothing but a function which contains methods and properties, so whenever you instantiate an object of JavaScript function /class it call that method so whatever you have initialize within the class will automatically be set. So you no need define separate constructor.
var Student = function () { console.log('Instance created'); this.prop_one = "initialized value"; this.prop_two = "initialized value"; }; var student = new Student(); console.log(student.prop_one); //initialized valueHere is Demo
Inheritance
Inheritance is one of the key feature of Object Oriented Programming, Inheritance can be achieved with prototype property. You can create a parent class and then inherit all the public methods and properties in child class. In JavaScript you do this by assigning an instance of the parent class to the child class. In ECMAScript 5 and above you can also use Object.create() to implement inheritance.
Lets see an example of inheritance in which we define a Human class and then inherit it in Student class and override some method of Human class.
// Define the Human constructor function Human(fullName) { this.fullName = fullName; } // Add a couple of methods to Human.prototype Human.prototype.speak = function(){ alert("I speak English!"); }; Human.prototype.introduction = function(){ alert("Hi, I am " + this.fullName); };
See fiddle demo here.
Now we will create a child class of student
// Define the Student function Student(fullName, school, courses) { //To initialize or call parent class constructor we need to Human.call() //for call() we need to pass this instance //since fullName is Human class property inherited in student so we //passed it to parent class Human.call(this, fullName); // Initialize our Student properties this.school = school; this.courses = courses; };
See fiddle demo here.
To inherit it from parent Human class we will set Student.prototype
//Do not try to set new Human() here as we are not calling it here //calling Human here need to pass fullName property which is not available here //the correct place to call parent object is in child class constructor //as we call it in above Student function code Student.prototype = Object.create(Human.prototype); // See note below // Set the "constructor" property to refer to Student Student.prototype.constructor = Student;As object.create() only works in browsers support ECMAScript 5 and above so to achieve this in older browser you can create your own function:
function createObject(prototype) { function emmptyClass() { } emmptyClass.prototype = prototype; return new emmptyClass(); } Student.prototype = createObject(Human.prototype);Lets see its usage:
// Example: var student = new Student("Ahmed","NED University", "Computer Science"); student.introduction(); // "Hi, I am Ahmed" student.speak(); // "I speak English!" // Check that instanceof works correctly alert(student instanceof Human); // true alert(student instanceof Student); // true
See here the demo.
Polymorphism / Overriding
Polymorphism is the main pillar of Object Oriented Programming, it refers to the ability of an object to provide different behaviors in different state of objects. For example Parent class Human define a function of introduction() in which it contains simple introduction but Student object override it by adding more detailed information for introduction. So now Human() object has its own implementation of introduction and Student() has its own. It will be invoked according to the state of object.
As we inherited the Human object in Student object we can override its public methods by changing its definition.
// override the "introduction" method Student.prototype.introduction= function(){ alert("Hi, I am " + this.fullName + ". I am a student of " + this.school + ", I study "+ this.courses +"."); };or you can also add new methods
// Add a "exams" method Student.prototype.takeExams = function(){ alert("This is my exams time!"); };
Here is usage of above overriden method:
var student = new Student("Ahmed","NED University", "Computer Science"); student.introduction(); // "Hi, I am Ahmed. I am a student of NED University, I study Computer Science." student.speak(); // "I speak English!" student.takeExams(); // "This is my exams time!" // Check that instanceof works correctly alert(student instanceof Human); // true alert(student instanceof Student); // true
See demo here
Encapsulation
Encapsulation is the one of the three pillars (Encapsulation, Inheritance and polymorphism) of OOP, It is a way of structuring data and methods by concealing the implementation or internal logic of object. Like in above example Human object contains the speak() method which is available in Student object but Student object does not know about the implementation of speak() method. It is encapsulated in Human object/class, child class inherit it as it is and change the methods which is different from its parent.
Summary
In this series of article we see how beautiful and impressive JavaScript is. It is as powerful as any other Object Oriented programming language is. In above series of article I used the words like namespace, class for objects which actually do not exist in JavaScript, just to map the JavaScript to the language you are aware of it. Please write your feedback about this series of articles in comments.