Created
July 20, 2017 08:40
-
-
Save askeing/d68993204c5aeedb574d140f3796a9c9 to your computer and use it in GitHub Desktop.
exercises of js-and-dom-for-gecko-hackers-talk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Source: https://github.com/mikeconley/js-and-dom-for-gecko-hackers-talk | |
site: https://askeing.github.io/js-and-dom-for-gecko-hackers-talk/ | |
slide-64 | |
*/ | |
function Shape({ x, y }) { | |
this._x = x; | |
this._y = y; | |
} | |
Shape.prototype = { | |
get coords() { | |
return { x: this._x, y: this._y }; | |
}, | |
} | |
// 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. | |
function Rect({x, y, h, w}) { | |
Shape.call(this, {x, y}); | |
this._h = h; | |
this._w = w; | |
this.isSquare = (this._h == this._w); | |
} | |
Rect.prototype = Object.create(Shape.prototype); | |
/* | |
// it can only be used by `s.isSquare()` | |
Rect.prototype.isSquare = function() { | |
if (this._h == this._w) { | |
return true; | |
} | |
return false; | |
}*/ | |
let s = new Rect({ x: 5, y: 5, h: 25, w: 25 }); | |
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 r of rects) { | |
console.log(r.isSquare); | |
} | |
/* | |
// Using object destructuring to assign the coords of | |
// s to local variables x and y in a single line. | |
*/ | |
let {_x: x, _y: y} = new Rect({ x: 16, y: -5, h: 3, w: 3 }); | |
console.log(x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment