js0005

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
<!DOCTYPE html>
<html>
<head>
<title>Storing a collection of unique functions</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../assert.css">
<script src="../assert.js"></script>
</head>
<body>
<script>
var store = {
nextId: 1,
cache: {},
add: function(fn) {
if (!fn.id) {
fn.id = this.nextId++;
this.cache[fn.id] = fn;
return true;
}
}
};

function ninja(){}

assert(store.add(ninja),
"Function was safely added.");
assert(!store.add(ninja),
"But it was only added once.");
</script>
</body>

</html>