-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter3-DataStructures.html
120 lines (90 loc) · 2.08 KB
/
chapter3-DataStructures.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<html>
<body>
<script type="text/javascript">
// Properties
var text="purple haze";
text["length"];
text.length;
// Objects: Think of them as a collection of properties
var cat =
{
color: "gray",
name: "Spot",
size: 46
};
cat.size;
delete cat.size;
var empty = {}
empty.notReally=100;
empty;
var chineseBox={};
chineseBox.content=chineseBox;
"content" in chineseBox;
// A set is a collection where no value appears more than once
// Mutability
var object1={value: 10};
var object2=object1;
var object3={value: 10};
object1==object2;
object1==object3;
object1.value=15;
object2.value;
object3.value;
//Objects as Collections: Arrays
var mailArchive=["mail one","mail two","mail three"];
for(var current=0; current < mailArchive.length; current++) {
print("processing email #", current, ": ", mailArchive[current]);
function range(upTo) {
var result=[];
for(var i=0; i <= upTo; i++) {
result[i]=i;
}
return result;
}
// Methods
var doh="Doh";
typeof doh.toUpperCase;
doh.toUpperCase();
var mack=[];
mack.push("Mack");
mack.push("the");
mack.push("Knife");
mack;
mack.join(" ");
mack.pop();
mack;
var words= "Cities of the Interior"
words.split();
var paragraph = "born 15-11-2003 (mother Spot): White Fang";
paragraph.charAt(0) == "b" && paragraph.charAt(1) == "o";
paragraph.slice(0,4) == "born";
function startsWith(string, pattern) {
return string.slice(0, pattern.length) == pattern;
}
startsWith("rotation", "rot");
"Pip".charAt(250);
// ""
"Nop".slice(1,10);
// "op"
// This chapter has an algoritm for parsing organized text
//Some more theory
// The arguments object
function argumentCounter() {
return "You gave me " + arguments.length + " arguments.";
}
argumentCounter("1","2","3");
// You gave me 3 arguments
//Polymorphism and function overriding
function range(start, end) {
if(arguments.length<2) {
end=start;
start=0;
}
var result=[];
for (var i=start; i <= end; i++) result.push(i);
return result;
}
// Java objects have Not Enumerable properties
</script>
</body>
</html>