Skip to content

Commit 8e846fa

Browse files
committed
新增字典数据结构
1 parent c0f4d3f commit 8e846fa

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

字典(Dictionary)/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<script src="./index.js"></script>
13+
</body>
14+
15+
</html>

字典(Dictionary)/index.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
3+
function Dictionary() {
4+
this.item = {}
5+
}
6+
7+
// has方法
8+
Dictionary.prototype.has = function (key) {
9+
return key in this.item
10+
}
11+
12+
// set方法
13+
Dictionary.prototype.set = function (key, value) {
14+
this.item[key] = value
15+
}
16+
17+
// get方法
18+
Dictionary.prototype.get = function (key) {
19+
return this.item[key]
20+
}
21+
22+
// 删除方法
23+
Dictionary.prototype.delete = function (key) {
24+
if (this.item.has(key)) {
25+
delete this.item[key]
26+
return true
27+
}
28+
return false
29+
}
30+
31+
// 清空方法
32+
Dictionary.prototype.clear = function () {
33+
this.item = {}
34+
}
35+
36+
// size方法
37+
Dictionary.prototype.size = function () {
38+
return Object.keys(this.item).length
39+
}
40+
41+
// 字典的所有键名
42+
Dictionary.prototype.keys = function () {
43+
return Object.keys(this.item)
44+
}
45+
46+
// 字典的所有键值
47+
Dictionary.prototype.keys = function () {
48+
return Object.values(this.item)
49+
}

0 commit comments

Comments
 (0)