Javascriptjs entry 2

Source: Internet
Author: User
1. An array defines an array. vararr1newArray () or vararr2 [] is not required in either of the two methods. Obviously, it is convenient to define an array for the second time. Define the array and initialize it. You can directly use vararr3 [& quot; abc & quot;, & quot; bcd & quot;, 18,... SyntaxHighlighter.

I. Array
Define an Array. You do not need to use var arr1 = new Array () or var arr2 = []. Obviously, it is convenient to define an Array for the second time.
Define an array and initialize it. You can directly use var arr3 = ["abc", "bcd", 18, 12.98, true]; // note that the array can store any type of data, not necessarily the same type of data.
Use the data name. length to obtain the array length. In the array, you can also directly arr3 [6] = "bdsaf"; in this way, the arr3 array will be automatically resized and changed to seven element arrays. Note that arrs [5] is an undefine constant.
Traverse the array:
For (var I = 0; I <arrs. length; I ++ ){
Document. write ("arrs [" + I + "] =" + arrs [I] +"
");
}
Ii. Functions
Functions, like arrays, are treated as objects (for example, alert (typeof (arr3 ));). The Function Definition Format is:
Function Name (function parameter list ){
Function body
}
You can call a function by using the function name (real parameter list). For example:
[Javascript]
Function add (x, y) {www.2cto.com
Return x + y
}
Call add (3, 4). Note that if you write in add (3, 5, 6), you can also call the above function. The returned structure is 8. After passing three parameters, will the parameters be lost in the function body. Actually, no. Because the function body has a built-in arguments array, this array records the input parameters. As long as you traverse this array, you can also get the third parameter 6.
The above mentioned function is also an object, so the function name is another address value referenced by this object. That is, you can perform the following operations.
Var ss = add; // This action will assign the address value of the add function to the ss variable. If alert (ss) is used, the toString method of the variable referenced by the ss is called, that is, the added toString prints out the function signature bodies.
In addition, if you call ss () like this, 12 is returned.
2.2 dynamic Functions
[Javascript]
Var add = new Function ("x, y", "var sum; sum = x + y; return sum ;");
 
Var he = add (4, 8 );

2.3 Anonymous Functions
It is usually used for events such as window onload or unonload. Of course, it can also be used as follows:
[Javascript]
Var add3 = function (a, B ){
Return a + B;
}
Alert (add3 (7,8 ));
Iii. scope of action of a Variable
. Generally, as long as the variables defined in the script are global variables, of course, the variables defined in the function body, except for the variables with the function parameters, they are local variables.
For example
[Javascript]
For (var I = 0; I <arrs. length; I ++ ){
 
Document. write ("arrs [" + I + "] =" + arrs [I] +"
");
 
}
// The variable I declared in the loop can be used outside the loop
Document. write ("I =" + I );
Iv. String object
You can directly define var str = "abc"; of course, you can also use var str = new String ("abc ");
The length of a String. You can directly use the length attribute. It is different from java. It is not a method. Alert (str. length );
Several common methods, str. bold (); will add a bold B label before and after the str character, str. fontcolor ("red"), you can specify the color of the str string text. Str. link (http: // ww...) can be linked.
For example, you can customize a function to Remove string spaces.
[Javascript] view plaincopy
/*
* The method of the string object in js is limited, and other functions of string operations are required.
* For example, remove spaces at both ends of a string. In this case, you can only customize it.
*/
// Remove spaces at both ends of the string.
Function trim (str ){
// Define two variables at the beginning of a record. The end position of a record.
// Judge the character at the starting position. If it is a space, it increments until it is not a space.
// Judge the character at the ending position. If it is a space, it will decrease until it is not a space.
// You must ensure that the start is <= ended to intercept the object.
Var start, end;
Start = 0;
End = str. length-1;

While (start <= end & str. charAt (start) = ''){
Start ++;
}
While (start <= end & str. charAt (end) = ""){
End --;
}
Return str. substring (start, end + 1 );
}
V. prototype attributes
Prototype: A description of the object. If new features are added to this description. This object will all have these new features. Prototype can obtain the prototype object. You can use prototype to expand the functions of objects.
For example, if you want to enhance the function of the String object and remove spaces, you can use the prototype to solve the problem. The Code is as follows:
[Javascript]
String. prototype. trim = trim; // get it done. Of course, if no trim has been defined, you have to do it yourself.
// For example, String. prototype. trim = function (xx ){...}
After this prototype is defined, the trim function may be directly used. For example:
[Javascript]
Alert ("-" + "AB cd". trim () + "-");
6. Basic Array Operations
[Javascript]
/*
* Demo array.
*/
Var arr = ["nba", "haha", "CBA", "aaa", "abc"];
Var arr2 = ["qq", "xiaoqiang", 70];
// Connect an element "mm" to an arr2 array.
Var newArr = arr. concat ("mm", arr2); // use mm as an element in the new array, and use the element in the arr2 array as an element in the new array.
 
Println (arr. join ("-"); // join method, which is connected to an array by default and can be replaced.
 
// Remove the element from the array and return it. Pop
 
Println (arr );
Println (arr. pop (); // Delete and return the last element.
Println (arr );
 
Println (arr. reverse ());
 
Println (arr. shift (); // Delete and return the first element.
Println (arr );
Var temp = arr. splice (, 9527, "xixixi", "wangcai"); // Delete the element and replace it.
Println (arr. unshift ("abcd3"); // Add abcd3.
Fucntion println (obj ){
Document. write (obj +"
")
}
8. The Array prototype is the same as the String prototype. You can enhance the Array object method and create multiple methods. For example
Array. prototype. getMax = function (){
Var tempIndex = 0;
For (var I = 1; I If (this [tempIndex] TempIndex = I;
}
}
Return this [tempIndex];
}
Var arr1 = [23,434, 11, 0]; var maxE = arr1.getMax (); // get it done.
9. Math object
Remember the floor method, ceil method, and pow method. In fact, these three methods are available in many languages. The floor is a low-end floor, so it is like Math. floor (12.34) will get 12. While
Ceil is a roof ceiling, relatively high-end, So Math. ceil (12.34) will get 13. Math. pow (100) returns. Note that all methods in Math are static.
10. Global Globe object method. In fact, no object exists. You can directly write the method without adding the object name above: for example, parseInt and isNaN methods:
[Javascript]
ParseInt ("123 ");
/*
* Demonstrate the global method of global.
*/
Println (parseInt ("123") + 1 );
 
Var val = parseInt ("12abc"); // val = 12;
 
Println ("value =" + isNaN (val); // checks whether the result is invalid through isNaN.
 
/*
* Convert a string in the specified hexadecimal format to decimal.
*/
Var num = parseInt ("110", 2 );
 
Println ("num =" + num );
 
Var num1 = parseInt ('0x3c ', 16 );
Println ("num1 =" + num1 );
 
// Convert decimal to other hexadecimal notation. Use a numeric object.
 
Var num3 = new Number (6 );
 
Println ("num3 =" + num3.toString (2 ));
 
Var num4 = 60;
Println ("num4 =" + num4.toString (16 ));

11. JS's new syntax for in structure displays one:
[Javascript]
Var arr = [32, 80, 65];
For (I in arr) {// If arr is an array, then I represents the badge in the array, not the element
Println ("I =" + arr [I]);
}
 
// Note that if the second arr location is set to an object set, I will be the element in the set.
 
.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.