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

推荐订阅源

Project Zero
Project Zero
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
S
Schneier on Security
N
News and Events Feed by Topic
N
News | PayPal Newsroom
H
Help Net Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
Attack and Defense Labs
Attack and Defense Labs
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
A
About on SuperTechFans
AWS News Blog
AWS News Blog
S
Secure Thoughts
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
C
Cybersecurity and Infrastructure Security Agency CISA
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
人人都是产品经理
人人都是产品经理
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
H
Hacker News: Front Page
P
Proofpoint News Feed
N
News and Events Feed by Topic
H
Heimdal Security Blog
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog

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一样去写静态类和静态属性。