-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathset.html
More file actions
68 lines (63 loc) · 2.3 KB
/
set.html
File metadata and controls
68 lines (63 loc) · 2.3 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// let set = new Set([1,2,3,4,4])
// console.log(set) //Set(4) {1,2,3,4}
// set.size 4
//操作方法:
// add(value) 添加某个值,返回Set结构本身
// delete(value) 删除某个值,返回布尔值,便是删除是否成功
// has(value) 返回一个布尔值,表示改制是否为Set的成员
// clear() 清除所有成员
// 遍历方法:
// keys(): 返回键名的遍历器
// values(): 返回键值的遍历器
// entries(): 返回键值对的遍历器
// forEach(): 使用回调函数遍历每个成员,无返回值
// 属性:
// Set.prototype.constructor: 构造函数,默认就是Set函数
// Set.prototype.size: 返回实例的成员总数
(function(global) {
function Set(data) {
this._values = []
this.size = 0;
data && data.forEach(function(item) {
this.add(item)
},this)
}
Set.prototype['add'] = function(value) {
if(this._values.indexOf(value) == -1) {
this._values.push(value);
++size;
}
return this;
}
Set.prototype['has'] = function(value) {
return (this._values.indexOf(value) !== -1)
}
Set.prototype['delete'] = function(value) {
var idx = this._values.indexOf(value);
if(idx == -1) return false
this._values.splice(idx,1);
return true
}
Set.prototype['clear'] = function(value) {
this._values = []
this.size = 0
}
Set.prototype['forEach'] = function(callBackFn, thisArg) {
thisArg = thisArg || global;
for(var i = 0; i < this._values.length;i++) {
callbackFn.call(thisArg,this._values[i],this._values[i],this)
}
}
})(this)
</script>
</body>
</html>