Arrays and functions in javascript

Array

Array definition The definition
of the array in js does not need to specify the type of the array. var a=[];You can directly define an empty array named a, and you can also var a=[1,"qwe","李"];directly add an array to it when defining it.
Array length The length of the
array is not limited when it is defined and can be changed at will. For example, when the empty array is first defined, the length is 0, you can add data to it to change the length of the array, a[2]=4;skip directly a[0] with subscript 0, and 1, a[1] adds the array to it, the length of the array at this time It becomes 3, and the values ​​of a[0] and a[1] are both undefine. Can be a.lengthused to test the length of the array.


function

The way of function definition
can use function or var keyword to define function. The
function 函数名(形参,形参,形参....){函数体}
var 函数名(形参,形参,形参....){函数体}
difference with java is that the parameter here does not need to specify the type, just pass it directly.
function 函数名(a,b,c){alert("有参函数被调用了");}
var 函数名(a,b,c){alert("有参函数被调用了");}

function b(){
    
    
//var b(){
    
    
		var a=150+200; 
		return a;
		}
		alert(b());

There is no need to define the return value type, just use it directly.

Functions in js cannot be overloaded. Unlike java, if you have to write in accordance with java's overloading method, the previous function will be overwritten by the latter function


The invisible parameter arguments in the function are only in the function defined by function.
Define a parameterless function function a(){}but it is not really parameterless. It has the invisible parameter argument, which is a bit like the side-length parameter in java. It is actually an array. We can pass any number of parameters to this parameterless function, the parameters of the data type of any parameter operation, these parameters are passed into argument[0],argument[1],argument[2]... ... ...it in order argument.length, and its length can be measured, and its length is the number of parameters passed in.

Guess you like

Origin blog.csdn.net/qq_45821251/article/details/108871118