/
index.html
87 lines (70 loc) 路 1.94 KB
/
index.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>What are Javascript symbols?</title>
<style>
</style>
</head>
<body>
<h1>What are Javascript symbols?</h1>
<i>The code for this example is in the script tag of this file.</i>
<script>
/*
const user = {
age: 29
}
const libOne = (obj) => {
obj.newProperty = 'Property from LibOne'
}
const libTwo = (obj) => {
obj.newProperty = function() {
return 'some string value from LibTwo'
}
}
// 馃槥 BAD: the property from libTwo will
// override the property from libOne
libOne(user)
libTwo(user)
console.log(user)
*/
/*
const uniquePropName = () => Math.random().toString(36).substring(7)
const user = {
age: 29
}
const libOne = (obj) => {
obj[uniquePropName()] = 'Property from LibOne'
}
const libTwo = (obj) => {
obj[uniquePropName()] = function() {
return 'some string value from LibTwo'
}
}
// 馃槓 SLIGHTLY BETTER: the properties from LibOne
// and LibTwo will not collide with each other,
// but their names will be random
libOne(user)
libTwo(user)
console.log(user)
*/
const user = {
age: 29
}
const libOne = (obj) => {
obj[Symbol('newProperty')] = 'Property from LibOne'
}
const libTwo = (obj) => {
obj[Symbol('newProperty')] = function() {
return 'some string value from LibTwo'
}
}
// 馃槂 GOOD: using Symbols the properties from LibOne
// and LibTwo will not colide, and we can
// the use the same description from both of them
libOne(user)
libTwo(user)
console.log(user)
</script>
</body>
</html>