Skip to content

Commit 175b100

Browse files
committed
Add examples
1 parent b437b55 commit 175b100

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

JavaScript/1-prototype.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
5+
function Rect(x, y, width, height) {
6+
this.x = x;
7+
this.y = y;
8+
this.width = width;
9+
this.height = height;
10+
}
11+
12+
Rect.prototype.toString = function() {
13+
return `[${this.x}, ${this.y}, ${this.width}, ${this.height}]`;
14+
};
15+
16+
function Square(x, y, side) {
17+
Rect.call(this, x, y, side, side);
18+
}
19+
20+
util.inherits(Square, Rect);
21+
22+
// Square.prototype = Object.create(Rect.prototype);
23+
// Square.prototype.constructor = Square;
24+
25+
// Square.prototype = new Rect();
26+
// Square.prototype.constructor = Square;
27+
28+
// Object.setPrototypeOf(Square.prototype, Rect.prototype);
29+
30+
const p1 = new Square(10, 20, 50);
31+
console.log(p1.toString());

JavaScript/2-class.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
class Rect {
4+
constructor(x, y, width, height) {
5+
this.x = x;
6+
this.y = y;
7+
this.width = width;
8+
this.height = height;
9+
}
10+
11+
toString() {
12+
return `[${this.x}, ${this.y}, ${this.width}, ${this.height}]`;
13+
}
14+
}
15+
16+
class Square extends Rect {
17+
constructor(x, y, side) {
18+
super(x, y, side, side);
19+
}
20+
}
21+
22+
const p1 = new Square(10, 20, 50);
23+
console.log(p1.toString());

JavaScript/3-function.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
5+
const rect = (x, y, width, height) => {
6+
return {
7+
x, y, width, height,
8+
toString: () => `[${x}, ${y}, ${width}, ${height}]`
9+
};
10+
};
11+
12+
const square = (x, y, side) => rect(x, y, side, side);
13+
14+
const p1 = square(10, 20, 50);
15+
console.log(p1.toString());

0 commit comments

Comments
 (0)