Continue to update. The general category is: function return value, function parameter passing, and array basics. You are welcome to ask questions and discuss them together :)
Function return value
If return exists and corresponding operations exist, the corresponding operation result is returned. See the following two cases:
1 <script> 2 function sum (a, B) 3 {4 return a + B; 5}; 6 alert (sum (2, 3 )); 7 </script> // return 5;
1 <script> 2 function sum (a, B) 3 {4 return; // The return method is not provided here. It returns undefined5 }; 6 alert (sum (2, 3); 7 </script>
Function Parameters
Uncertain parameter arguments
1 <script> 2 function total(){ 3 var result=0; 4 var i=0; 5 for(i=0;i<arguments.length;i++){ 6 result+=arguments[i]; 7 }; 8 alert(result); 9 };10 total(2,3,2,5,8,98);11 </script>
Using the above function, we can pass many parameters to total.
1 <script type = "text/javascript"> 2 window. onload = function () 3 {4 var oDiv = document. getElementById ('div1 '); 5 6 // obtain the calculated style (current style, final style) 7 // IE 8 // alert (oDiv. currentStyle. width); 9 10 // FF11 // alert (getComputedStyle (oDiv, false ). width); 12 13 if (oDiv. currentStyle) 14 {15 // Method 16 alert (oDiv. currentStyle. width); 17} 18 else19 {20 // FF method supported by Firefox 21 alert (getComputedStyle (oDiv, false ). width); the second value is optional, that is, the flase value 22} 23} 24 </script>
The preceding function can be used to obtain the non-interline style under IE and FF;
1 <style> 2 # div1 {width: 200px; height: 200px; background: red;} // set the div style; 3 </style> 4 <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> 5 <title> untitled document </title> 6 <script type =" text/javascript "> 7 function getStyle (obj, attr) 8 {9 if (obj. currentStyle) 10 {11 return obj. currentStyle [attr]; 12} 13 else14 {15 return getComputedStyle (obj, false) [attr]; 16} 17} // This function is similar to the above function Number, the simplified result. Name it getStyle18 19 function css (obj, attr, value) 20 {21 if (arguments. length = 2) // obtain the attribute 22 {23 return getStyle (obj, attr); 24} 25 else if (arguments. length = 3) // set the attribute 26 {27 obj. style [attr] = value; 28} 29} // This function is similar to the function 30 31 window in Jquery. onload = function () 32 {33 var oBtn = document. getElementById ('btn1 '); 34 var oDiv = document. getElementById ('div1 '); 35 36 oBtn. onclick = function () 37 {38 css (oDiv, 'background-color', 'green'); // it can only be a basic style and cannot contain a composite style like background, this property has attributes such as images, color, attchment, and position. The browser cannot identify 39 // alert (css (oDiv, 'width ')); 40} 41} 42 </script> 43
Array Basics
Define an arrayVar = arr [1, 2, 3, 4, 5];
Var = new Array (, 5); the first creation method is usually used, which is simple and clear.
<script> var arr=[1,2,3,4]; arr.length=10; alert(arr[5]);</script>
You can set the length of the array. If the length is smaller than the number of elements you define, extra values will be deleted. If the length is longer than the number of elements you set, undefined will pop up.
Add/delete an element push and pop from the end of the array
Add/delete an element unshift and shift from the array Header
Sort sorting
1 <script>2 var arr=[5,6,8,1,2,6,'love','about'];3 arr.sort();4 alert(arr);5 </script>
Because sort is too stupid, write a comparison function to sort values. However, an error occurred while running. You can find the error. Feature, found, function braces cannot add points.
1 var arr=[5,66,18,11,256,6];2 arr.sort(function(num1,num2){3 return num1-num2;4 });5 alert(arr);
Concat-- Connect two Arrays
var a=[1,2,3]; var b=[1,2,3,4]; alert(a.concat(b));
Join-- Add a separator (can only be a string)
var a=[1,2,3];alert(a.join('-'));
Split-- Converts a string into an array, but a string, not a value. (Split and separate)
var a='6-9-8-5';alert(a.split('-'));
Splice-- Delete an element from an array, add an element, and replace the element. (Paste)
1 var a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 2 // delete an element a from the second element. splice (); 3 // Delete 0 elements from the second element and add two elements. splice (, 'A', 'B'); 4 // Delete two elements, that is, delete three or four numbers, and add a, B, c. Three Elements. splice (2, 2, 'A', 'B', 'C'); 5 alert ();
END ~