Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View
JavaScript syntax 29 my_array=my_string.match(my_expression); // example my_array="We start at 11:30, 12:15 and 16:45".match(/\d\d:\d\d/); // my_array=("11:30","12:15","16:45"); Capturing groups var myRe = /(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})/; var results = myRe.exec("The date and time are 2009-09-08 09:37:08."); if(results) { alert("Matched: " + results[0]); // Entire match var my_date = results[1]; // First group == "2009-09-08" var my_time = results[2]; // Second group == "09:37:08" alert("It is " + my_time + " on " + my_date); } else alert("Did not find a valid date!"); Function Every function in Javascript is an instance of the Function object: var add=new Function('x','y','return x+y');//x,y is the argument.'return x+y' is the function body, which is the last in the argument list. var t=add(1,2); alert(t);//3 The add function above is often defined using the following pattern, as this pattern is faster and more intuitive. function add(x,y) { } return x+y; var t=add(1,2); alert(t);//3 A function instance has properties and methods. function subtract(x,y) { } return x-y; alert(subtract.length);//2,expected amount of arguments. alert(subtract.toString()); /* function subtract(x,y) { } */ return x-y;
JavaScript syntax 30 Operators The '+' operator is overloaded; it is used for string concatenation and arithmetic addition and also to convert strings to numbers. It also has special meaning when used in a regular expression. // Concatenate 2 strings var a = 'This'; var b = ' and that'; alert(a + b); // displays 'This and that' // Add two numbers var x = 2; var y = 6; alert(x + y); // displays 8 // Adding a string and a number results in concatenation alert(x + '2'); // displays 22 // Convert a string to a number var z = '4'; // z is a string (the digit 4) var x = '2'; // x is a string (the digit 2) alert(z + x); // displays 42 alert(+z + +x);// displays 6 Arithmetic JavaScript supports the following binary arithmetic operators: + Addition - Subtraction * Multiplication / Division (returns a floating-point value) % Modulus (returns the integer remainder) JavaScript supports the following unary arithmetic operators: + Unary conversion of string to number - Unary negation (reverses the sign) ++ Increment (can be prefix or postfix) -- Decrement (can be prefix or postfix) var x = 1; alert( ++x ); // displays: 2 alert( x++ ); // displays: 2 alert( x ); // displays: 3 alert( x-- ); // displays: 3 alert( x ); // displays: 2 alert( --x ); // displays: 1
- 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 and 28: JavaScript syntax 24 Native Objects
- Page 29 and 30: JavaScript syntax 26 } Math The Mat
- Page 31: JavaScript syntax 28 // ? - 0 or 1
- 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
- Page 79 and 80: Comparison of JavaScript-based sour
- Page 81 and 82: Comparison of JavaScript-based sour
JavaScript syntax 30<br />
Operators<br />
The '+' operator is overloaded; it is used for string concatenation and arithmetic addition and also to convert strings<br />
to numbers. It also has special meaning when used in a regular expression.<br />
// Concatenate 2 strings<br />
var a = 'This';<br />
var b = ' and that';<br />
alert(a + b); // displays 'This and that'<br />
// Add two numbers<br />
var x = 2;<br />
var y = 6;<br />
alert(x + y); // displays 8<br />
// Adding a string and a number results in concatenation<br />
alert(x + '2'); // displays 22<br />
// Convert a string to a number<br />
var z = '4'; // z is a string (the digit 4)<br />
var x = '2'; // x is a string (the digit 2)<br />
alert(z + x); // displays 42<br />
alert(+z + +x);// displays 6<br />
Arithmetic<br />
JavaScript supports the following binary arithmetic operators:<br />
+ Addition<br />
- Subtraction<br />
* Multiplication<br />
/ Division (returns a floating-point value)<br />
% Modulus (returns the integer remainder)<br />
JavaScript supports the following unary arithmetic operators:<br />
+ Unary conversion <strong>of</strong> string to number<br />
- Unary negation (reverses the sign)<br />
++ Increment (can be prefix or postfix)<br />
-- Decrement (can be prefix or postfix)<br />
var x = 1;<br />
alert( ++x ); // displays: 2<br />
alert( x++ ); // displays: 2<br />
alert( x ); // displays: 3<br />
alert( x-- ); // displays: 3<br />
alert( x ); // displays: 2<br />
alert( --x ); // displays: 1