-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuper.html
More file actions
51 lines (43 loc) · 1.05 KB
/
Copy pathsuper.html
File metadata and controls
51 lines (43 loc) · 1.05 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Static properties and methods in Widgets</title>
<script>
class Widget {
static counter = 0;
instanceField = "Webville Widgets";
#privateField = "Private Widget name";
constructor(type) {
this.type = type;
Widget.prototype.pField = "I'm a prototype field";
Widget.counter++;
}
toString() {
console.log(this.#privateField);
return `${ this.type } is a Widget with pField ${ this.pField }`;
}
static getCounter() {
return Widget.counter;
}
}
class SubWidget extends Widget {
constructor(type, value) {
super(type);
this.value = value;
}
fields() {
console.log(`Instance field: ${this.instanceField}`);
console.log(`Prototype field: ${ this.pField }`);
}
}
let widget = new Widget("green");
let subWidget = new SubWidget("blue", 2.50);
console.log(`Widget: ${widget.toString()}, counter: ${ Widget.getCounter() }`);
console.log(`Sub widget: ${subWidget.toString()}, counter: ${ SubWidget.getCounter() }`);
subWidget.fields();
</script>
</head>
<body>
</body>
</html>