惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
爱范儿
爱范儿
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
SecWiki News
SecWiki News
MyScale Blog
MyScale Blog
AI
AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Project Zero
Project Zero
T
Tor Project blog
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
IT之家
IT之家
The Hacker News
The Hacker News
腾讯CDC
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
C
Cisco Blogs
博客园 - 聂微东
Webroot Blog
Webroot Blog
Forbes - Security
Forbes - Security
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans

DaraW

GMTC 北京 2021 小程序开发实践专场所见所想 从一次 Bug 定位来看 Taro H5 的组件实现 浅谈 B 站新 BV 号的设计 复盘:Vue.js 是如何成功的 你可能不知道的 Git React 应用性能优化一例 Thrift RPC Mock 方案探索 前端视频质量监控 前端函数式编程 多说一句再见 《你不知道的JS上卷》阅读小记之setTimeout的this指向问题 IntelliJ IDEA在Linux下字体不正常解决方案 node-thunkify源码阅读笔记 如何监听JS变量的变化 JS实现自定义事件 制作列带有斑马条纹背景的表格 git用户名莫名其妙变化及挽救措施 记一次前端性能优化实战 将setTimeout函数队列
ES6实现内部类的写法
DaraW · 2017-01-18 · via DaraW

最近在把 JIris 移植到JS平台 Iris.js 的过程中不断的在Java和JS两种语言之间切换,ES6的 Class 某种程度来说可以写出更加优雅的代码,而不用去管背后的原型实现。但是不得不说有一个遗憾就是 ES6 虽然支持了静态方法,但是还不支持静态属性和静态类,于是仔细观察了下发现了几种ES6实现静态类的相对优雅的写法。

问题

简化一个Demo,先是 Java 的写法:
** IrisValue.java **

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
public class IrisValue {
private int mValue;

public IrisValue(int value) {
this.mValue = value;
}

public int getValue() {
return mValue;
}

public void setValue(int value) {
mValue = value;
}

static public class Test {
private int test = 0;

public int getTest() {
return test;
}

public void setTest(int value) {
test = value;
}
}

public static void main (String[] args) {
IrisValue irisValue = new IrisValue(10);
IrisValue irisValue2 = new IrisValue(10);

irisValue.setValue(100);

System.out.println(irisValue.getValue());
System.out.println(irisValue2.getValue());

Test testValue = new Test();
Test testValue2 = new Test();

System.out.println(testValue.getTest());
System.out.println(testValue2.getTest());
testValue.setTest(9);
testValue2.setTest(99);
System.out.println(testValue.getTest());
System.out.println(testValue2.getTest());
}

}

TestIrisValue类的静态方法,并不属于IrisValue的实例对象。上面代码执行结果是:

1
2
3
4
5
6
100
10
0
0
9
99

那么等同的JS代码应该怎么写呢?
ES6的Class本质还是函数,所以有一个很容易想到的写法:

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
class IrisValue {
constructor(value) {
this.mValue = value;
}

get value() {
return this.mValue;
}

set value(value) {
this.mValue = value;
}

}

class Test {
constructor() {
this.mTest = 0;
}

get test() {
return this.mTest;
}

set test(value) {
this.mTest = value;
}
}

IrisValue.Test = Test;


let irisValue = new IrisValue(10);
let irisValue2 = new IrisValue(10);

irisValue.value = 100;

console.log(irisValue.value);
console.log(irisValue2.value);

let testValue = new IrisValue.Test();
let testValue2 = new IrisValue.Test();

console.log(testValue.test);
console.log(testValue2.test);
testValue.test = 9;
testValue2.test = 99;
console.log(testValue.test);
console.log(testValue2.test);

输出结果和上面Java(预想的)一样,静态属性目前也能这样实现。

但是不要忘了get/set函数,所以应该有更加优雅的静态类和静态属性的写法:

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
class IrisValue {
constructor(value) {
this.mValue = value;
}

get value() {
return this.mValue;
}

set value(value) {
this.mValue = value;
}

static get Test() {
return Test;
}

}

class Test {
constructor() {
this.mTest = 0;
}

get test() {
return this.mTest;
}

set test(value) {
this.mTest = value;
}

}

其实我们还能再优雅一点~

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
class IrisValue {
constructor(value) {
this.mValue = value;
}

get value() {
return this.mValue;
}

set value(value) {
this.mValue = value;
}

static get Test() {
return class Test {
constructor() {
this.mTest = 0;
}

get test() {
return this.mTest;
}

set test(value) {
this.mTest = value;
}

};
}

}

其实上面的这些写法还是不够优雅,避免不了的牺牲了可读性。既然ES6已经引进了Class,期待以后能和Java一样去写静态类和静态属性。