Quantcast
Channel: JavascriptKata - Latest Comments
Viewing all articles
Browse latest Browse all 47

Re: How to inherit classes in javascript

$
0
0

Here is a simple way to implement inheritance in Javascript:
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');

alert(p.name);alert(pm.salary);View all code here with demos:
http://www.cyberminds.co.uk/bl...


Viewing all articles
Browse latest Browse all 47

Trending Articles