Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View
JavaScript syntax 25 You can use the object and array declaration literals to quickly create arrays that are associative, multidimensional, or both. cats = [{"color":"brown", "size":"large"}, {"color":"black", "size":"small"}]; cats[0]["size"]; // this gives you "large" dogs = {"rover":{"color":"brown", "size":"large"}, "spot":{"color":"black", "size":"small"}}; dogs["spot"]["size"]; // this gives you "small" dogs.rover.color; // this gives you "brown" Date A Date object stores a signed millisecond count with zero representing 1970-01-01 00:00:00 UT and a range of ±10 8 days. One can create a Date object representing "now": var d = new Date(); Initial value of Date object is current date and time. Other ways to construct a Date instance: var d = new Date(2010,4,5); // create a new Date instance with value: // 2010-4-5 00:00:00 var d = new Date(2010,4,5,14,25,30); // create a new Date instance with value: Format the display of a Date instance: var d = new Date(2010,5,6,14,25,30); // May,2010 // 2010-4-5 14:25:30 var display = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() d.getSeconds(); + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + alert(display); // Displays '2010-5-6 14:25:30' Construct from a String: var d = new Date("2010-4-6 14:25:30"); alert(d); Error Custom error messages can be created using the Error class: throw new Error("What the..."); Nested within conditional statements, such instantiations can substitute for try/catch blocks: var eMail = prompt("Please enter your e-mail address:", ""); if (!eMail || eMail.length == 0) { throw new Error("Excuse me: You must enter your e-mail address to continue.");
JavaScript syntax 26 } Math The Math object contains various math-related constants (e.g. π) and functions (e.g. cosine). (Note the "Math" object has no constructor, unlike Array or Date. All its methods are "static" aka "class" methods.) All the trigonometric functions use angles expressed in radians; not degrees or grads. Properties of the Math object Property Value Returned rounded to 5 digits Math.E 2.7183 e: Euler's number Description Math.LN2 0.69315 Natural logarithm of 2 Math.LN10 2.3026 Natural logarithm of 10 Math.LOG2E 1.4427 Logarithm to the base 2 of e Math.LOG10E 0.43429 Logarithm to the base 10 of e Math.PI 3.14159 π: circumference/diameter of a circle Math.SQRT1_2 0.70711 Square root of ½ Math.SQRT2 1.4142 Square root of 2 Example Value Returned rounded to 5 digits Methods of the Math object Math.abs(-2.3) 2.3 Absolute value: (x < 0) ? -x : x Math.acos(Math.SQRT1_2) 0.78540 rad. = 45° Arccosine Math.asin(Math.SQRT1_2) 0.78540 rad. = 45° Arcsine Math.atan(1) 0.78540 rad. = 45° Half circle arctangent (-π/2 to +π/2) Math.atan2(-3.7, -3.7) -2.3562 rad. = -135° Whole circle arctangent (-π to +π) Description Math.ceil(1.1) 2 Ceiling: round up to smallest integer ≥ argument Math.cos(Math.PI/4) 0.70711 Cosine Math.exp(1) 2.7183 Exponential function: e raised to this power Math.floor(1.9) 1 Floor: round down to largest integer ≤ argument Math.log(Math.E) 1 Natural logarithm, base e Math.max(1, -2) 1 Maximum: (x > y) ? x : y Math.min(1, -2) -2 Minimum: (x < y) ? x : y Math.pow(-3, 2) 9 Exponentiation (raised to the power of): Math.pow(x, y) gives x y Math.random() 0.17068 Pseudorandom number between 0 (inclusive) and 1 (exclusive) Math.round(1.5) 2 Round to the nearest integer; half fractions are rounded up (e.g. 1.5 rounds to Math.sin(Math.PI/4) 0.70711 Sine Math.sqrt(49) 7 Square root Math.tan(Math.PI/4) 1 Tangent 2)
- Page 1 and 2: Essentials of Javascript Visit the
- Page 3 and 4: John Resig 122 Reverse Ajax 123 Ric
- Page 5 and 6: JavaScript 2 History Anyway I know
- Page 7 and 8: JavaScript 4 Prototype-based protot
- Page 9 and 10: JavaScript 6 LCMCalculator.prototyp
- Page 11 and 12: JavaScript 8 Example - use in web p
- Page 13 and 14: JavaScript 10 request that might ha
- Page 15 and 16: JavaScript 12 Application platform
- Page 17 and 18: JavaScript 14 See also • Client-s
- Page 19 and 20: JavaScript 16 [25] Robert Nyman, Ge
- Page 21 and 22: JavaScript syntax 18 JavaScript syn
- Page 23 and 24: JavaScript syntax 20 Primitive data
- Page 25 and 26: JavaScript syntax 22 String A Strin
- Page 27: JavaScript syntax 24 Native Objects
- Page 31 and 32: JavaScript syntax 28 // ? - 0 or 1
- Page 33 and 34: JavaScript syntax 30 Operators The
- Page 35 and 36: JavaScript syntax 32 alert( Boolean
- Page 37 and 38: JavaScript syntax 34 Conditional op
- Page 39 and 40: JavaScript syntax 36 With The with
- Page 41 and 42: JavaScript syntax 38 Methods A meth
- Page 43 and 44: JavaScript syntax 40 Inheritance Ja
- Page 45 and 46: JavaScript syntax 42 See also • C
- Page 47 and 48: JavaScript Style Sheets 44 } fontSi
- Page 49 and 50: JavaScript engine 46 JavaScript eng
- Page 51 and 52: Ajax (programming) 48 Ajax (program
- Page 53 and 54: Ajax (programming) 50 • Ajax-powe
- Page 55 and 56: ?: 52 ?: In computer programming, ?
- Page 57 and 58: ?: 54 Furthermore, the ternary oper
- Page 59 and 60: ?: 56 ?: in style guidelines Some c
- Page 61 and 62: Appcelerator Titanium 58 See also
- Page 63 and 64: Bookmarklet 60 Bookmarklet A bookma
- Page 65 and 66: Bookmarklet 62 References [1] Domai
- Page 67 and 68: Client-side JavaScript 64 implement
- Page 69 and 70: Client-side JavaScript 66 Reference
- Page 71 and 72: dhtmlx [1] Comparison of JavaScript
- Page 73 and 74: ility / ion Comparison of JavaScrip
- Page 75 and 76: Comparison of JavaScript frameworks
- Page 77 and 78: Comparison of JavaScript frameworks
JavaScript syntax 25<br />
You can use the object and array declaration literals to quickly create arrays that are associative, multidimensional,<br />
or both.<br />
cats = [{"color":"brown", "size":"large"},<br />
{"color":"black", "size":"small"}];<br />
cats[0]["size"]; // this gives you "large"<br />
dogs = {"rover":{"color":"brown", "size":"large"},<br />
"spot":{"color":"black", "size":"small"}};<br />
dogs["spot"]["size"]; // this gives you "small"<br />
dogs.rover.color; // this gives you "brown"<br />
Date<br />
A Date object stores a signed millisecond count with zero representing 1970-01-01 00:00:00 UT and a range <strong>of</strong> ±10 8<br />
days. One can create a Date object representing "now":<br />
var d = new Date();<br />
Initial value <strong>of</strong> Date object is current date and time. Other ways to construct a Date instance:<br />
var d = new Date(2010,4,5); // create a new Date instance with<br />
value:<br />
// 2010-4-5 00:00:00<br />
var d = new Date(2010,4,5,14,25,30); // create a new Date instance with<br />
value:<br />
Format the display <strong>of</strong> a Date instance:<br />
var d = new Date(2010,5,6,14,25,30); // May,2010<br />
// 2010-4-5 14:25:30<br />
var display = d.getFullYear() + '-' + (d.getMonth()+1) + '-' +<br />
d.getDate()<br />
d.getSeconds();<br />
+ ' ' + d.getHours() + ':' + d.getMinutes() + ':' +<br />
alert(display); // Displays '2010-5-6 14:25:30'<br />
Construct from a String:<br />
var d = new Date("2010-4-6 14:25:30");<br />
alert(d);<br />
Error<br />
Custom error messages can be created using the Error class:<br />
throw new Error("What the...");<br />
Nested within conditional statements, such instantiations can substitute for try/catch blocks:<br />
var eMail = prompt("Please enter your e-mail address:", "");<br />
if (!eMail || eMail.length == 0)<br />
{<br />
throw new Error("Excuse me: You must enter your e-mail address to<br />
continue.");