Skip to content

Instantly share code, notes, and snippets.

@yagopv
Last active January 3, 2016 10:58
Show Gist options
  • Save yagopv/8452525 to your computer and use it in GitHub Desktop.
Save yagopv/8452525 to your computer and use it in GitHub Desktop.
JavaScript Inheritance
//**************************************************************
var Vehicle = (function(){
function Vehicle(model, year, engineSize) {
this.model = model;
this.year = year;
this.engineSize = engineSize;
var engineIsStarted = false;
this.isStarted = function () { return engineIsStarted; }
this.start = function () { engineIsStarted = true; }
this.stop = function () { engineIsStarted = false; }
}
return Vehicle;
}());
var Car = (function (parent) {
Car.prototype = new Vehicle();
Car.prototype.constructor = Car;
function Car(model, year, engineSize, wheelCount) {
parent.call(this, model, year, engineSize);
this.wheelCount = wheelCount;
}
return Car;
}(Vehicle));
var Boat = (function (parent) {
Boat.prototype = new Vehicle();
Boat.prototype.constructor = Boat;
function Boat(model, year, engineSize, width, length) {
parent.call(this, model, year, engineSize);
this.width = width;
this.length = length;
}
return Boat;
}(Vehicle));
//*****************************************************************
var a = {
first : 'Hello',
second : 'World',
sum : function(x, y){
return x + y;
}
};
var b = {
difference : function(x, y){
return x - y;
},
__proto__ : a
};
var c = {
product : function(x, y){
return x * y;
},
__proto__ : a
};
//*****************************************************************
(function (Module) {
var _age;
function Animal(name, description, age) {
this.name = name;
this.description = description;
_age = age;
}
Animal.prototype.addYear = function() {
_age++;
}
Animal.prototype.getAge = function() {
return _age;
}
Module.Animal = Animal;
})(window.Module || (window.Module = {}));
(function (Module) {
function Tigre(name, description, age, nrayas) {
Module.Animal.call(this, name, description, age);
this.nrayas = nrayas;
}
Tigre.prototype = new Module.Animal;
Module.Tigre = Tigre;
})(window.Module || (window.Module = {}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment