-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path静态属性的使用.js
More file actions
38 lines (30 loc) · 965 Bytes
/
静态属性的使用.js
File metadata and controls
38 lines (30 loc) · 965 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
function Web(url) {
this.url = url; //动态属性
}
Web.url = '1024bibi.com'; //静态属性
let hd = new Web('1024bat.cn');
console.log(hd.url); // 1024bat.cn
console.log(Web.url); // 1024bibi.com
console.log(hd); // Web { url: '1024bat.cn' }
console.log(Web); // [Function: Web] { url: '1024bibi.com' }
class User {
host = 'sssssss';
}
let HD1 = new User();
let HD2 = new User();
HD2.host = 'HD2.host';
console.log(HD1.host); //sssssss
console.log(HD2.host); //HD2.host
class User1 {
//静态属性即为类设置属性,而不是为生成的对象设置,下面是原理实现
//在 class 中为属性添加 static 关键字即声明为静态属性
//如果属性是为所有对象共用的 不是为某一对象使用 可以定义为静态属性
static host = 'HD3.host';
getHost() {
return User1.host
}
}
let HD3 = new User1();
console.log(HD3.host); //undefined
console.log(User1.host); //HD3.host
console.log(HD3.getHost()); //HD3.host