Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active August 5, 2018 04:03
Show Gist options
  • Select an option

  • Save danilobatistaqueiroz/deca8f6a573d32731e8df08c31774120 to your computer and use it in GitHub Desktop.

Select an option

Save danilobatistaqueiroz/deca8f6a573d32731e8df08c31774120 to your computer and use it in GitHub Desktop.
ES5 classical javascript inheritance
// Shape - superclass
// x,y: location of shape's bounding rectangle
function Shape(x, y) {
this.x = x;
this.y = y;
}
// Superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
}
// Circle - subclass
function Circle(x, y, r) {
// Call constructor of superclass to initialize superclass-derived members.
Shape.call(this, x, y);
// Initialize subclass's own members
this.r = r;
}
// Circle derives from Shape
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.constructor = Circle;
// Subclass methods. Add them after Circle.prototype is created with
// Object.create
Circle.prototype.area = function() {
return this.r * 2 * Math.PI;
}

###First old style:

function Something() {
   this.depth = 0;
}
Something.prototype.incDepth = function() {
   this.depth++;
}

foo = new Something()

###Second old style:

function login () { 
	return {
		get: function(req,res,next) {
		}
	}
	return {
		post: function(req,res,next) {
		}
	}
}

var a = login()

###The new way:

class Something {
  constructor() {
    this.depth = 0;
  }
  inc() {
    this.depth++;
  }
}
let foo = new Something();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment