Last active
December 16, 2015 16:49
-
-
Save miguelmota/5466003 to your computer and use it in GitHub Desktop.
Prototype in JavaScript. Blog post: http://www.miguelmota.com/blog/understanding-prototype-in-javascript
This file contains 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
//---------------------------------- | |
// Prototype Demo | |
// --------------------------------- | |
// Define functional object constructor called Quadrilateral | |
// with width and height as parameters. | |
var Quadrilateral = function(width, height) { | |
this.width = width; | |
this.height = height; | |
return this; | |
} | |
// Define method to return width. | |
Quadrilateral.prototype.getWidth = function() { | |
return this.width; | |
} | |
// Define new object of type Quadrilateral. | |
var mysquare = new Quadrilateral(4,4); | |
// Call the getWidth method. | |
console.log(mysquare.getWidth()); // returns 4 | |
// Define method to set dimensions. | |
Quadrilateral.prototype.setDimensions = function(width, height) { | |
this.width = width; | |
this.height = height; | |
return this; | |
} | |
// Set the dimensions of mysquare. | |
mysquare.setDimensions(7,7); | |
// Output mysquare width. | |
console.log(mysquare.getWidth()); // returns 7 | |
// Define functional object constructor called Rectangle | |
// with width and height as parameters. | |
var Rectangle = function(width, height){ | |
this.width = width; | |
this.height = height; | |
return this; | |
}; | |
// Inherit methods and properties from Quadrilateral object. | |
Rectangle.prototype = new Quadrilateral(); | |
// Define new object of type Rectangle. | |
var myrectangle = new Rectangle(6,2); | |
// Output myrectangle width. | |
console.log(myrectangle.getWidth()); // returns 6 | |
// Test Quadrilateral and Rectangle method properties | |
console.log(Quadrilateral.prototype.hasOwnProperty('getWidth')); // returns true | |
console.log(Rectangle.prototype.hasOwnProperty('getWidth')); // returns false | |
// Test mysquare inheritance | |
console.log(mysquare instanceof Quadrilateral); // returns true | |
console.log(mysquare instanceof Rectangle); //return false | |
// Test myrectangle inheritance | |
console.log(myrectangle instanceof Quadrilateral); // returns true | |
console.log(myrectangle instanceof Rectangle); //return true | |
// Test inheritance from global Object object | |
console.log(Quadrilateral instanceof Object); // returns true | |
console.log(Rectangle instanceof Object); //return true | |
console.log(mysquare instanceof Object); // returns true | |
console.log(myrectangle instanceof Object); //return true | |
// Check if a global method exists, if not create our own | |
if (!Array.prototype.forEach) { | |
Array.prototype.forEach = function(fn, scope) { | |
for(var i = 0, len = this.length; i < len; ++i) { | |
fn.call(scope, this[i], i, this); | |
} | |
} | |
} | |
//EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment