Rectangle class and Square class - Node.js Object

Node.js examples for Object:Class Definition

Description

Rectangle class and Square class

Demo Code


/******************************************************************************/
// <<: new//  w ww  .j  ava  2 s . c  o m
var Rectangle = function(width, height) {
  this.width  = width;
  this.height = height;
};

Rectangle.prototype.area = function() {
  return this.width * this.height;
};

var Square = function(width) {
  var rect = new Rectangle(width, width);
  rect.isSquare = true;
  return rect;
};

var sq = new Square(10);
console.log(sq.area());
// :>>

/******************************************************************************/
// <<: create
var Rectangle = function(width, height) {
  var rect = Object.create(Rectangle.prototype);
  rect.width  = width;
  rect.height = height;
  return rect;
};

Rectangle.prototype.area = function() {
  return this.width * this.height;
};

var Square = function(width) {
  var rect = Rectangle(width, width);
  rect.isSquare = true;
  return rect;
};

var sq = Square(10);
console.log(sq.area());

Related Tutorials