Skip to content

Instantly share code, notes, and snippets.

@gufranco-zz
Last active December 19, 2015 04:29
Show Gist options
  • Save gufranco-zz/5897760 to your computer and use it in GitHub Desktop.
Save gufranco-zz/5897760 to your computer and use it in GitHub Desktop.
JavaScript Prototypal Inheritance
;$(function() {
var car = new Car();
car.checkEngine(); // vehicle - engine checked
car.openDriversDoor(); // car - door opened
car.closeDriversDoor(); // car - door closed
car.startEngine(); // vehicle - engine started
car.stopEngine(); // vehicle - engine stopped
var motorcycle = new Motorcycle();
motorcycle.checkEngine(); // vehicle - engine checked
motorcycle.startEngine(); // vehicle - engine started
motorcycle.startStunt(); // motorcycle - start stunt
motorcycle.stopStunt(); // motorcycle - stop stunt
motorcycle.stopEngine(); // vehicle - engine stopped
});
/**
* Car object
*
* @author Gustavo Franco
* @since 2013-06-30
*/
;var Car = (function(Vehicle, $, window, document, undefined) {
'use strict';
/**
* Constructor method
*
* @author Gustavo Franco
* @since 2013-06-30
*/
function Car() {
this._isDoorOpened = false;
}
/**
* Inherite the vehicle's prototype
*
* @author Gustavo Franco
* @since 2013-07-01
*/
Car.prototype = new Vehicle();
/**
* Open the driver's door
*
* @author Gustavo Franco
* @since 2013-07-01
*/
Car.prototype.openDriversDoor = function() {
try {
if (this._isEngineStarted) {
throw new Error('You can\'t open the door while the engine is running.');
}
if (this._isDoorOpened) {
throw new Error('The door is already opened.');
}
console.log('car - door opened');
this._isDoorOpened = true;
} catch (exception) {
console.error(exception.message);
}
};
/**
* Close the driver's door
*
* @author Gustavo Franco
* @since 2013-07-01
*/
Car.prototype.closeDriversDoor = function() {
try {
if (this._isEngineStarted) {
throw new Error('You can\'t close the door while the engine is running.');
}
if (!this._isDoorOpened) {
throw new Error('The door is already closed.');
}
console.log('car - door closed');
this._isDoorOpened = false;
} catch (exception) {
console.error(exception.message);
}
};
return Car;
})(Vehicle, jQuery, window, document);
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Prototypal Inheritance</title>
</head>
<body>
<p>Open the console ;)</p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="Vehicle.js"></script>
<script src="Car.js"></script>
<script src="Motorcycle.js"></script>
<script src="bootstrap.js"></script>
</body>
</html>
/**
* Motorcycle object
*
* @author Gustavo Franco
* @since 2013-06-30
*/
;var Motorcycle = (function(Vehicle, $, window, document, undefined) {
'use strict';
/**
* Constructor method
*
* @author Gustavo Franco
* @since 2013-06-30
*/
function Motorcycle() {
this._isDoingStunt = false;
}
/**
* Inherite the vehicle's prototype
*
* @author Gustavo Franco
* @since 2013-07-01
*/
Motorcycle.prototype = new Vehicle();
/**
* Start a stunt
*
* @author Gustavo Franco
* @since 2013-07-01
*/
Motorcycle.prototype.startStunt = function() {
try {
if (!this._isEngineStarted) {
throw new Error('You must start the engine first.');
}
if (this._isDoingStunt) {
throw new Error('You are already doing a stunt.');
}
console.log('motorcycle - start stunt');
this._isDoingStunt = true;
} catch (exception) {
console.error(exception.message);
}
};
/**
* Stop a stunt
*
* @author Gustavo Franco
* @since 2013-07-01
*/
Motorcycle.prototype.stopStunt = function() {
try {
if (!this._isDoingStunt) {
throw new Error('You must start a stunt first.');
}
console.log('motorcycle - stop stunt');
this._isDoingStunt = false;
} catch (exception) {
console.error(exception.message);
}
};
return Motorcycle;
})(Vehicle, jQuery, window, document);
/**
* Vehicle object
*
* @author Gustavo Franco
* @since 2013-06-30
*/
;var Vehicle = (function($, window, document, undefined) {
'use strict';
/**
* Constructor method
*
* @author Gustavo Franco
* @since 2013-06-30
*/
function Vehicle() {
this._isEngineChecked = false;
this._isEngineStarted = false;
}
/**
* Check the engine
*
* @author Gustavo Franco
* @since 2013-06-30
*/
Vehicle.prototype.checkEngine = function() {
try {
if (this._isEngineChecked) {
throw new Error('The engine is already checked.');
}
console.log('vehicle - engine checked');
this._isEngineChecked = true;
} catch (exception) {
console.error(exception.message);
}
};
/**
* Start the engine
*
* @author Gustavo Franco
* @since 2013-06-30
*/
Vehicle.prototype.startEngine = function() {
try {
if (!this._isEngineChecked) {
throw new Error('You must check the engine first.');
}
if (this._isEngineStarted) {
throw new Error('The engine is already started.');
}
console.log('vehicle - engine started');
this._isEngineStarted = true;
} catch (exception) {
console.error(exception.message);
}
};
/**
* Stop the engine
*
* @author Gustavo Franco
* @since 2013-06-30
*/
Vehicle.prototype.stopEngine = function() {
try {
if (!this._isEngineStarted) {
throw new Error('You must start the engine first.');
}
console.log('vehicle - engine stopped');
this._isEngineStarted = false;
} catch (exception) {
console.error(exception.message);
}
};
return Vehicle;
})(jQuery, window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment