The sixth web front-end training notes (HTML)

I array
Definition of array
1. Implicit definition
Var array name = []; 1 / empty array var array name = [value 1, value 2, value 3...];
2. Direct instantiation
var array name = new array (value 1, value 2, value 3...);
3. Define the array and set the length
var array name = new array (size);
                    
Array operation
The subscript of the array starts from 0 (no subscript out of bounds)
Gets the value of the specified subscript of the array: (undefined if the subscript does not exist)
Array name [subscript];
Set the value of the specified subscript of the array: (if the subscript does not exist, it will be automatically expanded)
Array name [subscript] = value;
Gets the length of the array
Array name length;
Set the length of the array:
Array name length = value
Understand
If a non integer subscript is set, it will become an attribute of the array and will not be included in the length of the array
Set properties:
Array name Attribute name = value;
Array name ["attribute name"] = value;
Get properties:
Array name Attribute name;
Array name ["attribute name];

Traversal of array
                1. for loop traversal
For (VaR index = 0; index < array length; index + +){
                        
                    }
Equivalent to Java:
For (int index = E; index < array length; index + +){
                2. for . . .in cycle
for(var subscript in array){
                        
                    }
                3. forEach loop
Array forEach(function(element , index){
/ / element: element
/ / - index: subscript
                   });
                
Note:
for -- does not traverse attributes
foreach -- does not traverse undefined in attributes and indexes
for- in -- · do not traverse undefined in index

Methods provided by array:
push # add element to last
index0f# array element index
Convert join array to string
split string method: converts a string into an array
unshift: add element to top
pop # delete the last item
shift delete the first item
reverse array flip
slice intercepts (slices) the array, and the original array does not change
splice , splicing array, the original array changes, which can achieve the effect of deletion before and after
concat array merge

 <script type="text/javascript">
		 /*Definition of array*/
		 //Implicit definition
		 var arr1 =[];
		 console. log( arr1);
		 var arr2= [1,2, "a" ,true];
		 console.log( arr2);
		 
		 //Direct instantiation
		 var arr3 = new Array ( 1,2,3);
		 console.log( arr3);
		 
		 //Define the array and set the length
		 var arr4 = new Array(5);
		 console.log(arr4);
		 
		 console.log("=-=---=--===");/*Array operation */
		 //Gets the value of the specified subscript
		 console.log( arr2[1]);// 2
		 //If the subscript does not exist
		 console.log(arr2[10]); // undefined
		 //Sets the value of the specified subscript
		 arr2[1]=20;
		 console.log ( arr2);
		 //If the subscript does not exist
		 arr2[10] = 100;
		 console.log( arr2); 
		//Gets the length of the array
		console.log( arr3. length); // 3
		  //Sets the length of the array
		arr3. length = 5;
		console.log(arr3);
		//Set the properties of the array
		arr3.name= "zhangsan" ;
		console.log(arr3);
		arr3 [ "pwd" ] ="123456";
		console. log( arr3);
		//Gets the properties of the array
		console. log(arr3[ "name" ]);


		 console. log("----------	==");
		 /*Traversal of array*/
		 console. log(arr3);
		 console. log("----for Loop traversal----" );
		 // for loop traversal
		 for( var i = 0; i < arr3.length; i++) {
			console.log("Indexes:" + i +",Value:"+ arr3[i])
		}
		console. log("----for...in----" );
		//for. . .in
		console.log("- --for . . .in----");for (var i in arr3) {
		console. log("subscript:" +i + ",value:"+arr3[i]);
		}
		// forEach
		console.log("---forEach-- --" );
		arr3.forEach(function(element,index){
		console.log("subscript:" + index +",value: "+ element);
		})

		 /*Array provided method*/
		 // push add element to last
		 // index0f array element index
		 // Convert join array to string
		 // split string method: converts a string into an array

		  console. log("----------	==");
		  var arr5 = ["a","b","c"];
		  // push to add meta cable to the end
		  arr5[arr5.length] = "d";
		  arr5. push("e");
		  console. log(arr5);
		  
		  // indexOf array element index
		  console. log(arr5. indexOf("a")); // 0
		  console. log(arr5. indexOf("t")); // -1, not found return - 1

		  // Convert join array to string
		  console. log(arr5.join("-")); // a-b-c-d-e

		  // split string method: converts a string into an array
		  var str = "1,2,3,4,5";
		  console. log(str  .split(","));

		  
		  
		 </script>

II function

Definition of function
            1. Function declaration statement
Function function name ([parameter list]){
            }
            2. Function definition expression
var variable name / function name = function([parameter list]){
            }
            3. Function constructor (understand)
var function name = new Function([parameter list], function body);
                
Parameters of function
Set formal parameters when defining a function, and pass arguments when calling a function.
            1. If the argument can be omitted, the formal parameter is undefined
            2. If the formal parameter names are the same, the last parameter shall prevail
            3. You can set the default value of the parameter
            4. If the parameter is value transfer, transfer the copy; If the parameter is passed by reference, the address is passed and the same object is operated on
            
Function call
            1. Common call mode
Function name ([parameter list]);
            2. Function call mode (function has return value)
var variable name = function name ([parameter list]);
            3. Method invocation mode
Object Function name ([parameter list]);
Return value of function
            return
            1. In a method that does not return a value, it is used to end the method. (if the method has no return value, use return to return undefined
            2. Among the methods with return value, one is to end the method and the other is to bring the value to the caller.

Scope of function
In JS, scope is only available in functions.
            1. In a function, there are local and global variables
            2. In a function, if the var modifier is not used when declaring a variable, the variable is a global variable

 <script type="text/javascript">
		 
		 
		 /*Definition of function */
		 // 1. Function declaration statement
		 function fn01(a,b) {
		 			console.log(a+b);
		 }
		 console. log(fn01);
		 // 2. Function definition expression
		 var fn02 = function(a,b) {
		 			console.log(a+b);
		 }
		 console. log(fn02);
		 // 3. Function constructor
		 var fn03 = new Function('a','b','return (a+b);');
		  console. log(fn03);
		  //amount to
		  function fn04(a,b) {
		  return (a + b);
		 }
		 
		 
		 /* Parameters of function */
		 // 1. If the argument can be omitted, the formal parameter is undefined
		 
		 
		 function test01(x,y){
		 	console.log(x+y);
		 }
		 //Call function
		 //Argument not set
		 test01(); // NaN
		 test01(1); // NaN
		 test01(1,2); // 3
		 
		 // 2. If the formal parameter names are the same, the last parameter shall prevail
		 function test02(x,x) {
		 	console.log(x);
		 	}
		 test02(1,2); // 2
		 // 3. You can set the default value of the parameter
		 function test03(x) {
		 	// If the formal parameter x has a value, the value is the value passed by X; Otherwise, the value is "X"
		 	x= x || "x";
		 	console.log(x);
		 }
		 test03(10); // 10
		 test03(); // X
		 function test04(x) {
		 (x != null && x != undefined) ?x=x:x="x" ;
		 console.log(x);
		 }
		 test04(); // x
		 test04("Hello");
		 // 4. The parameter is value transfer, and the copy is transferred; If the parameter is passed by reference, the address is passed and the same object is operated on
		 //pass by value
		 var num=10;//variable
		 // //Function
		  function test05 (num) {
		  num = 20;
		  }
		  //Call function
		  test05(num); //Arguments are defined variables
		  console.log(num);
		  
		  //Reference passing
		  var obj = {name: "zhangsan"};
		  console.log(obj);
		  function test06(o) {
		  o.name = "lisi";
		  }
		  test06(obj);
		  console. log(obj);
		  
		  
		  console.log("==============")
		  /* Function call */
		  //Common call mode
		  function f1() {
		  console. log("Common call mode...");
		  }
		  f1();
		  //Function call mode
		  var f2 = function(a) {
		  console. log("Function call mode...");
		  return a
		  }
			var num1 = f2(1);
			console. log(num1);
			//Method invocation mode
			//Definition object key: string value value: any data type
		var obj={
			name: "zhangsan", //character string
			age:18,//numerical value
			sex:true, //Boolean
			cats:["Big hair","twenty fen"],//array
			dog:{ //object
				name :"Zhang Ergou",
				age:1
			},
			sayHello:function(){ //function
				console. log("How do you do~");
			},
			}
			console. log(obj);
			console. log(obj. sayHello()); //Object call function
			obj. sayHello()//Object call function



			console. log("============");
			/*Return value of function*/
			function a1(){
				console.log("no return value...");
				return;
				console.log('........');
				}
			console.log(a1());
			
			function a2 (){
			console. log("There is a return value...");
			return "test";
			}
			console.log(a2());

			 console.log("===============");
			 /*Scope of function*/
			 var pp = "Hello"; //global variable
			 //The variables defined in the function are local variables
			 function qq() {
				//local variable
				var box = 10;
				// Global variables are not decorated with var
				box2 = 20;  
			 }
			 // Call the method and execute the code in the method
			 qq();
			 console.log(pp); //global variable
			 /* console.log(box);      box Is a local variable*/
			 console.log(box2);

		 </script>

Keywords: Javascript Front-end html linq

Added by apan on Thu, 10 Feb 2022 23:05:14 +0200