How to implement inheritance in JavaScript
2014-02-11 13:38
447 查看
Introduction
If you have done Object Oriented Programming in JavaScript, you will know that you can create a class as follows:Person = function(id, name, age){ this.id = id; this.name = name; this.age = age; alert('A new person has been accepted'); }
So far our class
Persononly has two properties and we are going to give it some methods. A clean way of doing this is to use its '
prototype' object.
Starting from JavaScript 1.1, the
prototypeobject was introduced in JavaScript. This is a built in object that simplifies the process of adding custom properties and methods to all instances of an object.
Let's add two methods to our class using its '
prototype' object as follows:
Person.prototype = { /** wake person up */ wake_up: function() { alert('I am awake'); }, /** retrieve person's age */ get_age: function() { return this.age; } }
Now we have defined our class
Person. What if we wanted to define another class called
Managerwhich inherits some properties from
Person? There is no point redefining all these properties again when we define our
Managerclass, we can just set it to inherit from the class
Person.
JavaScript doesn't have built-in inheritance but we can use a technique to implement inheritance as follows:
Inheritance_Manager = {};//We create an inheritance manager class (the name is arbitrary)
Now let's give our inheritance class a method called
Extendwhich takes the
baseClassand
subClassasarguments. Within the
Extendmethod, we will create an inner class with a function
inheritance() { }. The reason why we are using this inner class is to avoid confusion between the
baseClassand
subClassprototypes.
Next we make the prototype of our inheritance class point to the
baseClassprototype as in the following code:
inheritance.prototype = baseClass. prototype;
Then we copy the inheritance prototype into the
subClassprototype as follows:
subClass.prototype = new inheritance();
The next thing is to specify the constructor for our
subClassas follows:
subClass.prototype.constructor = subClass;
Once finished with our
subClassprototyping, we can specify the next two lines of code to set some base class pointers.
subClass.baseConstructor = baseClass; subClass.superClass = baseClass.prototype;
Here is the full code for our
Extendfunction:
Inheritance_Manager.extend = function(subClass, baseClass) {
function inheritance() { }
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass; subClass.superClass = baseClass.prototype;
}
Now that we have implemented our inheritance, we can start using it to extend our classes. In this case, we are going to extend our
Personclass into a
Managerclass as follows:
We define the
Managerclass:
Manager = function(id, name, age, salary) { Person.baseConstructor.call(this, id, name, age); this.salary = salary; alert('A manager has been registered.'); }
We make it inherit form
Person:
Inheritance_Manager.extend(Manager, Person);
If you have noticed, we have just called the
Extendmethod of our
Inheritance_Managerclass and passed the
subClassManager in our case and then the
baseClassPerson. Note that the order is very important here. If you swap them, the inheritance will not work as you intended if at all.
Also note that you will need to specify this inheritance before you can actually define the
subClass.
Now let us define the
subClass:
We can add more methods as shown below. Our
Managerclass will always have the methods and properties defined in the
Personclass because it inherits from it.
Manager.prototype.lead = function(){ alert('I am a good leader'); }
Now to test it, let us create two objects, one from the class
Personand one from the inherited class
Manager:
var p = new Person(1, 'Joe Tester', 26); var pm = new Manager(1, 'Joe Tester', 26, '20.000');
Here is the full JavaScript code you can copy:
Person = function(id, name, age){
this.id = id;
this.name = name;
this.age = age;
alert('A new person has been accepted');
}
Person.prototype = { /** wake person up */ wake_up: function() { alert('I am awake'); }, /** retrieve person's age */ get_age: function() { return this.age; } }
Inheritance_Manager = {};
Inheritance_Manager.extend = function(subClass, baseClass) {
function inheritance() { }
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass; subClass.superClass = baseClass.prototype;
}
Manager = function(id, name, age, salary) {
Manager.baseConstructor.call(this, id, name, age);
this.salary = salary;
alert('A manager has been registered.');
}
Inheritance_Manager.extend(Manager, Person);
Manager.prototype.lead = function(){ alert('I am a good leader'); }
var p = new Person(1, 'Joe Tester', 26); var pm = new Manager(1, 'Joe Tester', 26, '20.000');
You can manipulate these objects by accessing some of their properties as follows:
alert(p.name); alert(pm.salary);
转自:http://www.codeproject.com/Articles/303796/How-to-Implement-Inheritance-in-Javascript
相关文章推荐
- What is the difference between the ways to implement inheritance in javascript.
- several way to implement inheritance in javascript
- How to implement xpcom module in JavaScript
- How to implement scrolling image with javascript in web page
- HOW-TO: Debug JavaScript in Internet Explorer(step by step)
- How to implement master-details in OAF?
- Yii 2.0: How to implement cron in Yii 2
- What is EPOLL? EPOLL vs Select call? And How to implement UDP server in Linux using EPOLL?
- How to use composition and inheritance in visual c# ?
- [iOS开发站在巨人肩膀上]之How to implement Queue in Objective-C
- how to add Javascript and CSS in page layout with sharepoint 2013
- How to implement *All-Digital* analog-to-digital converters in FPGAs and ASICs
- How to implement realtime periodic tasks in Linux applications
- How to implement multiple channels in wifi module of ns3
- [iOS开发站在巨人肩膀上]之How to implement a stack in Objective-C
- How To Work With JSON In Node.js / JavaScript
- What is EPOLL? Epoll vs Poll vs Select call ? And How to implement UDP server in Linux using EPOLL?
- How to implement the built in effects of DirectX.DirectSound
- how to implement gesture detector onfling in webview in android - Stack Overflow
- How to clone an object in JavaScript