Last active
January 3, 2016 10:58
-
-
Save yagopv/8452525 to your computer and use it in GitHub Desktop.
JavaScript Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//************************************************************** | |
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