-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1-prototype.js
More file actions
36 lines (27 loc) · 806 Bytes
/
1-prototype.js
File metadata and controls
36 lines (27 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
function Rect(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
Rect.prototype.toString = function() {
return `[${this.x}, ${this.y}, ${this.width}, ${this.height}]`;
};
function Square(x, y, side) {
Rect.call(this, x, y, side, side);
}
Object.setPrototypeOf(Square.prototype, Rect.prototype);
// Square.prototype = Object.create(Rect.prototype);
// Square.prototype.constructor = Square;
// Square.prototype = new Rect();
// Square.prototype.constructor = Square;
// For node.js
// util.inherits(Square, Rect);
// Usage
const p1 = new Square(10, 20, 50);
console.dir(Square.prototype);
console.dir(p1.prototype);
console.dir(p1.constructor === Square);
console.dir(p1.__proto__ === Square.prototype);
console.log(p1.toString());