Javascript inheritance: A simple example








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

Person.prototype.walk = function() {
    alert(this.name + ' is walking.');
}

var Driver = function(name) {
    Person.call(this,name);
}

Driver.prototype = Object.create(Person.prototype);

Driver.constructor = Driver;

Driver.prototype.drive = function(){
    alert(this.name + ' is driving.');
}

var dave = new Person('David');
dave.walk();

var john = new Driver('John');
john.drive();



No comments:

Post a Comment