Skip to content

Instantly share code, notes, and snippets.

@gasolin
Created March 7, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gasolin/7e94910078cf94981812 to your computer and use it in GitHub Desktop.
Save gasolin/7e94910078cf94981812 to your computer and use it in GitHub Desktop.
function Shape({ x, y }) {
this._x = x;
this._y = y;
}
Shape.prototype = {
get coords() {
return { x: this._x, y: this._y };
},
}
function Rect({x, y, h, w}) {
Shape.call(this, {x, y});
this._h = h;
this._w = w;
}
Rect.prototype = Object.create(Shape.prototype);
Object.defineProperty(Rect.prototype, 'isSquare', {
get: function() {
return this._x === this._y && this._x === this._h && this._x === this._w;
}
});
// Implement Rect, which subclasses Shape. In addition to
// the x and y coordinate parameters, should also take height
// and width. Should also have a isSquare getter that returns
// true if the Rect is square.
let s = new Rect({ x: 15, y: 25, h: 5, w: 5 });
console.log(s.isSquare);
let rects = [
new Rect({ x: 0, y: 0, h: 5, w: 100 }),
new Rect({ x: 0, y: 5, h: 13, w: 2 }),
new Rect({ x: 10, y: 10, h: 15, w: 15 }),
new Rect({ x: 16, y: -5, h: 3, w: 3 }),
];
// Iterate the rects using for...of, and console.log
// isSquare for each.
for (let x of rects) {
console.log(x.isSquare);
}
/*
// Using object destructuring to assign the coords of
// s to local variables x and y in a single line.
*/
let {x, y} = s.coords;
console.log(x, y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment