forked from AK-47-D/nodejs_es6_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes6_class_demo.js
More file actions
53 lines (41 loc) · 888 Bytes
/
Copy pathes6_class_demo.js
File metadata and controls
53 lines (41 loc) · 888 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 在 Node 中使用模块的正确姿势:
const log = require("./lib/util_for_node");
class Product {
constructor(name, price) {
this.name = name
this.price = price
}
list() {
return [
new Product("iPad Pro 2018", 10000),
new Product('iPhone XMax', 9000),
]
}
}
const main = () => {
const p = new Product()
const list = p.list()
log(list)
}
main();
/**
* 输出:
$ node es6_class_demo.js
[ Product { name: 'iPad Pro 2018', price: 10000 },
Product { name: 'iPhone XMax', price: 9000 } ]
*/
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Dog extends Animal {
speak() {
console.log(this.name + ' barks.');
}
}
var d = new Dog('Mitzie');
d.speak(); // Mitzie barks.