-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathprotected.js
More file actions
36 lines (30 loc) · 925 Bytes
/
protected.js
File metadata and controls
36 lines (30 loc) · 925 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
/**
*将属性定义为以 _ 开始,来告诉使用者这是一个私有属性,请不要在外部使用。
外部修改私有属性时可以使用访问器 setter 操作
但这只是提示,就像吸烟时烟盒上的吸烟有害健康,但还是可以抽的
*/
// 继承时是可以使用的
const HOST = Symbol('主机'); //使用symbol定义protected属性
class Article {
[HOST] = 'https://xxx.com';
set host(url) {
if (!/^https:\/\//i.test(url)) {
throw new Error('网址错误');
}
this[HOST] = url;
}
lists() {
return `${this[HOST]}/article`;
}
}
class Common extends Article {
lists() {
return `${this[HOST]}/article`;
}
}
let common = new Common
console.log(common.lists()); // https://xxx.com/article
let article = new Article();
console.log(article.lists()); //https://xxx.com/article
article.host = 'https://xxx.com';
console.log(article.lists()); //https://xxx.com/article