Javascript method overridden

Best example of method overridden is here..

<html>Method override
<body>
<script>
// 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 Vehicle.prototype.go.call(this) + ” car”
}

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

var c = new Car(“red”);
c.go() // “Underway in red car”
console.log(c.go());
</script>
</body>
</html>

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s