Javascript inheritance example

<html>Inheritance
<body>
<script>
// Create a class
var vehicle = function(modelname){
var that = {};
that.engine = “suzuki”;
that.tyres = “MRF”;
that.modelname = modelname;
that.getModel = function(){
console.log(this.modelname+”has engine”+that.engine);
}
return that;
}

var twowheeler = function(model){
var that = vehicle(model);
that.type = “two wheeler”;
return that;
}

var glamour = twowheeler(“g20”);
glamour.engine; // should display suzuki
console.log(glamour.engine);
glamour.getModel(); // display g20 has suzuki
console.log(glamour.getModel());
</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