Skip to content

Instantly share code, notes, and snippets.

@batogov
Created October 1, 2017 16:21
Show Gist options
  • Save batogov/2a8ef90a97d797541b202c3b9de0f1ae to your computer and use it in GitHub Desktop.
Save batogov/2a8ef90a97d797541b202c3b9de0f1ae to your computer and use it in GitHub Desktop.
Prototypes
/* POINT */
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.move = function(deltaX, deltaY) {
this.x += deltaX;
this.y += deltaY;
}
Point.prototype.constructor = Point;
/* POINT WITH NAME */
function PointWithName(x, y, name) {
Point.call(this, x, y);
this.name = name;
}
PointWithName.prototype = Object.create(Point.prototype);
PointWithName.prototype.construstor = PointWithName;
/* CUSTOM NEW */
function customNew(constr, args) {
var thisValue = Object.create(constr.prototype);
constr.apply(thisValue, args);
return thisValue;
}
/* OBJECT CREATION */
var p = customNew(Point, [1, 2]);
var pWithName = customNew(PointWithName, [1, 2, 'Awesome Point']);
console.log(p);
console.log(pWithName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment