1. Conversion between basic string types and string objects
2. String object
3. Regular Expression
4. Array
5. Functions
6. Anonymous Functions
7. Function literal
1. Conversion between basic string types and string objects
If you create a String of the basic type, but access is performed by the object, JavaScript will automatically convert the basic type to an object, but the String object to be converted is only a temporary variable, in addition, this object is destroyed after the attribute operation, so this operation is not effective enough, and there is only one conversion process.
Var strName = "Shelley"; // basic string type
Alert (strName. length); // implicitly create a String object. The value is the same as the value of strName and the length method is executed.
2. String object
Reference in this section: JavaScript String object reference manual
Http://www.w3school.com.cn/js/jsref_obj_string.asp
Var sObject = new String ("Sample string ");
A string is a basic data type of JavaScript. The length attribute of the String object declares the number of characters in the String. The String class defines a large number of String operations, such as extracting characters or substrings from a String, or retrieving characters or substrings.
It should be noted that JavaScript strings are immutable, and the methods defined by the String class cannot change the content of strings. A method like String. toUpperCase () returns a brand new String instead of modifying the original String.
In the earlier JavaScript Implementation of Netscape code base (such as Firefox implementation), the string behavior is like a read-only character array. For example, to extract the third character from string s, use s [2] instead of s. charAt (2 ). In addition, when a for/in loop is applied to a string, it will enumerate the array subscript of each character in the string (but note that the length attribute cannot be enumerated according to ECMAScript standards ). Because the string array behavior is not standard, avoid using it.
3. Regular Expression
A regular expression is an expression composed of strings. It is used to match, replace, or search for a specific string. You can use the RegExp object to explicitly create a regular expression:
Var searchPatten = new RegExp ('s + ');
You can also create a regular expression by using the text volume:
Var searchPatten =/s + /;
Test Method
The test method checks whether the string passed in with the parameter matches the regular expression.
Var re =/Javascript rules/I;
/* Var re = new RegExp ('s + ', 'G'); // The object instance. The second parameter indicates the matching option */
Var str = "Javascript rules ";
If (re. test (str) document. writeln ("I guess it does rule ");
Modifier
I. Perform case-insensitive matching.
G ).
M.
Exec Method
Var re = new RegExp ("JS *", "ig ");
Var str = "cfdsJS * (& YJSjs 888JS ";
Var resultArray = re.exe c (str );
While (resultArray ){
Document. writeln (resultArray [0]);
Document. writeln ("next match starts at" + re. lastIndex + "<br/> ");
ResultArray = re.exe c (str );
}
/*
Because the option g is set, the lastIndex attribute in RegExp is set to the position of the last match, so every exec call will find the next match. In this example, a total of four matches are found. If no match is found, the return value is null, And the loop ends automatically when the array is null.
Output:
JS next match starts at 6
JS next match starts at 13
Js next match starts at 15
JS next match starts at 21
*/
The exec method returns an array, but the array does not have all the matching items, but the current match and all the substrings with parentheses. If you use parentheses in an expression to reference a part of a regular expression, the matching strings of these parentheses are also reflected in the returned array.
Var re =/(ds) + (j + s)/ig;
Var str = "cfdsJS * (& dsjjjsYJSjs 888 dsdsJS ";
Var resultArray = re.exe c (str );
While (resultArray ){
Document. writeln (resultArray [0]);
Document. writeln ("next match starts at" + re. lastIndex + "<br/> ");
For (var I = 1; I <resultArray. length; I ++)
{
Document. writeln ("substring of" + resultArray [I] + "<br/> ");
}
Document. writeln ("<br/> ")
ResultArray = re.exe c (str );
}
/*
Output:
DsJS next match starts at 6
Substring of ds
Substring of JS
Dsjjjs next match starts at 16
Substring of ds
Substring of jjjs
DsdsJS next match starts at 31
Substring of ds
Substring of JS
*/
Methods for String objects that support regular expressions
Search retrieves the value that matches the regular expression.
Match finds matching of one or more regular expressions.
Replace replaces the substring that matches the regular expression.
Split splits the string into a string array.
Reference: JavaScript RegExp Object Reference Manual
Http://www.w3school.com.cn/js/jsref_obj_regexp.asp
4. Array
Arrays are not necessarily one-dimensional. In JavaScript, the method for managing multi-dimensional arrays is to create a new array for each array element.
Var threedPoints = new Array ();
ThreedPoints [0] = new Array (1.2, 3.33, 2.0 );
ThreedPoints [1] = new Array (5.3, 5.5, 5.5 );
ThreedPoints [2] = new Array (6.4, 2.2, 1.9 );
Var newZPoint = threedPoints [2] [2]; // The array is accessed as an index.
The concat and slice of the array do not change the original array, but create a new array as the return value of the method.
In most cases, the order of array elements is not important, but there are also some scenarios that need to maintain the order of array elements, such as queues. Methods for maintaining queues in Arrays:
Push: add elements to the end of the array
Unshift adds the element to the beginning of the array
Pop removes the last element of the array.
Shift remove the first element
Array access
// Traverse the array through Loops
For (var I = 0; I <threedPoints [0]. length; I ++ ){
Alert (threedPoints [0] [I]);
}
// Use the forin Loop
For (var itemIndex in threedPoints [0]) {
Document. writeln (threedPoints [0] [itemIndex] + "<br/> ");
}
Create an array using a comma-separated string
Var animalString = "cats, dogs, birds, horse ";
Var animalArray = animalString. split (",");
Alert (animalArray [2]); // alert box display birds
JavaScript Array Object Reference Manual
Http://www.w3school.com.cn/js/jsref_obj_array.asp
5. Functions
Functions in JavaScript are similar to objects. You can define a function, create a new function, or even output a function. It is precisely because of this function that you can give a function to a variable or array element, or even pass it as a parameter to another function call.
There are three methods to create functions in JavaScript: declarative/static, dynamic/anonymous, and literal. Before using them, it is very important to understand the effects of various methods.
If you need to execute multiple tasks in a function, you can consider splitting the function into several smaller units, which improves reusability. In practice, it is a rule that should be followed to make the function as short as possible, to make it special to a specific character, and to keep it generic as much as possible.
6. Anonymous Functions
Anonymous/dynamic functions are a good way to define a function that can determine the requirement at runtime. Anonymous functions are parsed once each access. Use the anonymous function constructor:
Var variable = new Function ("param1", "param2",..., "paramn", "function body ");
/*
You can use the alert dialog box to set the function body and two parameters required for defining the function,
Then call this function,
Output the result to the page.
Note: IE7 is abnormal when FF debugging is passed.
*/
// Enter the function body and parameters.
Var func = prompt ("Enter function body :");
Var x = prompt ("Enter value of x :");
Var y = prompt ("Enter value of y :");
// Call this anonymous Parameter
Var op = new Function ("x", "y", func );
Var theAnswer = op (x, y); // function anonymous (x, y ){}
// Output the function execution result
Alert ("Function is:", func );
Alert ("x is:" + x +
"; Y is:" + y );
Alert ("The answer is:" + theAnswer );
/*
Input/output:
Function is: return x * y;
X is: 33; y is: 11;
The answer is: 363;
*/
7. Function literal
The function literal is also called a function expression, because the function created in this way will become part of the expression, rather than a special type of statement, they do not define the function name like an anonymous function, however, the function literal is parsed only once, and it is static.
When you want to implement an extension such as a function as a parameter of another function, the features of the Function literal volume will be displayed.
// Declare the third parameter as a function
Function funcObject (x, y, z ){
Alert (z (x, y ));
}
// The third parameter is a function.
FuncObject (3, 4, function (x, y) {return x * y })