This page (revision-10) was last changed on 15-Apr-2016 14:22 by BlakeMcBride

This page was created on 25-Oct-2014 08:46 by BlakeMcBride

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
10 15-Apr-2016 14:22 6 KB BlakeMcBride to previous
9 18-Dec-2015 08:39 6 KB BlakeMcBride to previous | to last
8 04-May-2015 09:37 6 KB BlakeMcBride to previous | to last
7 12-Jan-2015 18:30 5 KB BlakeMcBride to previous | to last
6 12-Jan-2015 18:28 5 KB BlakeMcBride to previous | to last
5 12-Jan-2015 12:33 4 KB BlakeMcBride to previous | to last
4 12-Jan-2015 12:27 4 KB BlakeMcBride to previous | to last
3 12-Jan-2015 09:41 4 KB BlakeMcBride to previous | to last
2 25-Oct-2014 09:02 4 KB BlakeMcBride to previous | to last
1 25-Oct-2014 08:46 12 bytes BlakeMcBride to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

1!!! JavaScript
2
3The JavaScript language is a general purpose language that is most often, but not necessarily, run within an Internet browser. Except for the name, JavaScript is completely unrelated to Java.
4
5!! The Language
6
7All symbols in JavaScript are case-sensitive.
8
9! Data types
10
11| String | 'Hello'
12| String | 'Hello' (prefer this form)
13| | "Hello"
14| Number | 45
15| | 3.14
16| Boolean | true/false
17| null | null
18| undefined | undefined
19| NaN | not a number
20| RegExp | /.*/g
21| Object (JSON) | {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}
22| Array | {{{[33, 'Dog', 55.6]}}}
23
24
25All numbers are floating point using a format that exactly represents integers (no rounding error for integers).
26
27There is no character type.
28
29Strings are represented with double-quotes OR single-quotes, and characters can be escaped with a backslash.
30
31You can Concatenate strings with +, i.e.:
32
33{{{"abc" + "def"}}}
34
35
36! Boolean values and null
37
38Two constants "true" and "false"
39
40Result of comparisons, i.e.:
41
42{{{x == y}}}
43
44"null" is also a constant that means no value
45false, null, undefined, NaN, 0, and "" are all treated as "false". Anything else is treated as "true".
46
47The {{{===}}} operator is used to tell if the two arguments are the same object (not just equal).
48
49! Variables
50
51Variable names must start with a letter, underscore, or $, and may contain all of those characters plus numbers thereafter.
52
53Variables are type-less. You can assign a number, string, etc. to any variable at any time.
54
55Declare with "var" like this:
56
57{{{var a;
58var b = 44;
59var c = 'hello';
60var d, e=7, f;}}}
61
62Be very careful! Most JavaScript's will allow you to assign a value to an undeclared variable. It will seem to work but you have unknowingly created a GLOBAL variable rather than one local to the function you are in. ALWAYS declare variables!
63
64Variables are local to the context in which they are declared. This means if you declare it outside of a function, it is global. If you declare it inside a function it is local to that function. Keep all variables as local as possible.
65Variables are either global or local to the function in which they are declared. This means if you declare it outside of a function, it is global. If you declare it inside a function it is local to that function. Keep all variables as local as possible. {} blocks do not establish a variable context / scope in JavaScript.
66
67
68! Functions
69
70There are two ways of declaring a function as follows ("function" is a keyword):
71
72{{{function myFunc(x, y) {
73 return x + y;
74}
75}}}
76
77The above is pretty traditional. Notice that none of the variables have types. Elsewhere, you can call the above function in an expected way, i.e.:
78
79{{{myFunc(3, 4);}}}
80
81Or, to use the return value:
82
83{{{var r = myFunc(3, 4);}}}
84
85Another way to create a function is through a variable syntax. Although you may think "what good is that?", it is extremely important, and it opens the door to some very, very important things. The above function can also be defined as follows:
86Another, equivalent, way to create a function is through the variable syntax. Although you may think "what good is that?", it is extremely important, and it opens the door to some very, very important things. The above function can also be defined as follows:
87
88{{{var myFunc = function(x, y) {
89 return x + y;
90}
91}}}
92
93This makes the following about JavaScript clear:
94
951. All functions in JavaScript are just variables holding function values!
96
972. Function values can be passed around and assigned just like variables, i.e.:
98
99{{{function abc(x, y) {
100 return x + y;
101}
102
103var def = function(x, y) {
104 return x * y;
105}
106
107print(abc(3, 4));
108print(def(3, 4));
109
110var g = abc;
111var h = def;
112
113print(g(4, 5));
114print(h(4, 5));
115}}}
116
117So, it doesn't matter which way you declare the function, it is the same. The value of declaring it as a variable, however, is to make it clear what is really is - a variable!
118
119Inside function are all of the standard stuff like "if", "else", "while", "do", "for", "return", "switch", "continue", "break", "throw", "try/catch", "with", etc..
120
121
122! Arrays
123
124Arrays in JavaScript are zero based indexing.
125
126{{{var myArray = [36, 45, 'hello'];
127
128print(myArray[0]);
129print(myArray[1]);
130print(myArray[2]);
131
132myArray[1] = 'dog';
133
134print(myArray[0]);
135print(myArray[1]);
136print(myArray[2]);
137
138print(myArray.length);
139
140// You can auto-extend an array:
141
142myArray[3] = 'new element';
143
144print(myArray[3]);
145print(myArray.length);
146
147myArray[43] = 'far away';
148print(myArray[43]);
149print(myArray.length); // returns 44}}}
150
151
152! Objects
153
154Objects are really hash tables!
155
156
157{{{var x = new Object();
158
159x['George'] = 44; // key=George, value=44
160x['sam'] = 27;
161x['marry'] = 33;
162
163print(x['George']);
164print(x['sam']);
165
166// alternate way of doing the same thing
167
168var x = new Object();
169
170x.George = 55;
171x.sam = 66;
172x.marry = 77;
173
174print(x.George);
175print(x.sam);
176
177// you can even assign them one way and access them in the other
178
179
180print(x['George']);
181print(x['sam']);
182
183print(Object.keys(x).length); // the number of elements
184
185// Enumerating through all the keys
186
187for (var k in x)
188 print(k);
189
190
191// Enumerating through all the values
192
193for (k in x)
194 print(x[k]);
195
196
197// A very important way of initializing an object:
198
199var y = { abc: 104, def: 5.5, hij: 'hello' };
200
201print(y["abc"]);
202print(y.def);
203print(y.hij);
204}}}
205
206!! Classes
207
208{{{
209function MyClass(init) {
210 this.iv1 = init;
211 this.iv2 = 0;
212}
213
214MyClass.cv1 = 101;
215
216MyClass.getCV1 = function() {
217 return MyClass.cv1;
218}
219
220MyClass.setCV1 = function(val) {
221 MyClass.cv1 = val;
222}
223
224MyClass.prototype.getIV1 = function() {
225 return this.iv1;
226}
227
228MyClass.prototype.setIV1 = function(val) {
229 this.iv1 = val;
230}
231
232
233var x = new MyClass(36);
234var y = new MyClass(42);
235
236print(x.getIV1());
237print(y.getIV1());
238print(MyClass.cv1);
239
240x.setIV1(44);
241y.setIV1(22);
242MyClass.setCV1(202);
243
244print(x.getIV1());
245print(y.getIV1());
246print(MyClass.getCV1());
247}}}
248
249
250
251! Inheritance
252
253{{{
254function Mammal(name) {
255 this.name = name;
256}
257
258Mammal.prototype.getName = function() {
259 return this.name;
260}
261
262function Cat(name, color) {
263 Mammal.call(this, name); // call the super constructor to inherit its instance vars
264 this.color = color;
265}
266
267Cat.prototype = Object.create(Mammal.prototype); // copy parent prototype to
268 // inherit its methods
269 // this must be done before
270 // assigning local methods
271
272Cat.prototype.getColor = function() {
273 return this.color;
274}
275
276var a = new Cat("Woolfy", "Brown");
277
278print(a.getName());
279print(a.getColor());
280
281}}}
282