Javascript method overridden

Best example of method overridden is here..

<html>Method override
// Create a class
function Vehicle(color){
this.color = color;

// Add an instance method
Vehicle.prototype.go = function(){
return “Underway in ” + this.color;

// Add a second class
function Car(color){
this.color = color;

// And declare it is a subclass of the first
Car.prototype = new Vehicle();

// Override the instance method
Car.prototype.go = function(){
return + ” car”

// Create some instances and see the overridden behavior.
var v = new Vehicle(“blue”);
v.go() // “Underway in blue”

var c = new Car(“red”);
c.go() // “Underway in red car”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s