Skip to content

Instantly share code, notes, and snippets.

Created August 12, 2016 01:04
Show Gist options
  • Save anonymous/0e63dbbec5f1966381625cf8991d1273 to your computer and use it in GitHub Desktop.
Save anonymous/0e63dbbec5f1966381625cf8991d1273 to your computer and use it in GitHub Desktop.
Constructor code along.. geometry ex // source http://jsbin.com/leketu
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Constructor code along.. geometry ex</title>
</head>
<body>
<script id="jsbin-javascript">
// 1) Create a Rectangle constructor that has "length" and "width" instance properties
// 2) Assign Rectangle.prototype a function, getArea, that returns the rectangle's area
// 3) Create a Square constructor that has a "size" instance property
// 4) Square should inherit from the Rectangle constructor
// Bonus: Use Rectangle's constructor inside of Square
function Rectangle(length, width){
this.width = width;
this.length = length;
}
Rectangle.prototype.getArea = function(){
console.log( this.length * this.width);
}
//Square inherits from Rectangle
function Square (size){
this.length = size;
this.width = size;
}
Square.prototype = new Rectangle();
var rect = new Rectangle(4,10);
var square = new Square(5);
square.getArea();
rect.getArea();
</script>
<script id="jsbin-source-javascript" type="text/javascript">// 1) Create a Rectangle constructor that has "length" and "width" instance properties
// 2) Assign Rectangle.prototype a function, getArea, that returns the rectangle's area
// 3) Create a Square constructor that has a "size" instance property
// 4) Square should inherit from the Rectangle constructor
// Bonus: Use Rectangle's constructor inside of Square
function Rectangle(length, width){
this.width = width;
this.length = length;
}
Rectangle.prototype.getArea = function(){
console.log( this.length * this.width);
}
//Square inherits from Rectangle
function Square (size){
this.length = size;
this.width = size;
}
Square.prototype = new Rectangle();
var rect = new Rectangle(4,10);
var square = new Square(5);
square.getArea();
rect.getArea();</script></body>
</html>
// 1) Create a Rectangle constructor that has "length" and "width" instance properties
// 2) Assign Rectangle.prototype a function, getArea, that returns the rectangle's area
// 3) Create a Square constructor that has a "size" instance property
// 4) Square should inherit from the Rectangle constructor
// Bonus: Use Rectangle's constructor inside of Square
function Rectangle(length, width){
this.width = width;
this.length = length;
}
Rectangle.prototype.getArea = function(){
console.log( this.length * this.width);
}
//Square inherits from Rectangle
function Square (size){
this.length = size;
this.width = size;
}
Square.prototype = new Rectangle();
var rect = new Rectangle(4,10);
var square = new Square(5);
square.getArea();
rect.getArea();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment