1. Creation of functions
Since a lot of functions have been used before, I won’t share more here.
2. Nested functions
function china()
{
function people()//Nested function, only china uses it
{
document.write("My wish is to be Du Fu. Recently, his comics are online. He Live so freely");
}
people();
}
3. Direct quantities of functions
Javascript allows functions to use direct quantities Definition, to put it bluntly, is an expression (anonymous function). Details: Anonymous functions will be used in the future, please understand!
function china(people) //Function declaration
{
return peole;
}
var fun = function(people){return people;};//Declared by expression. The effect is the same
4. Function parameters (everyone should pay attention here)
When you call a function with less than the declared number of parameters, the other parameters are undefined value.
//Print function
function displayArray(arr)
{
if(!arr)return;
for(var num =0;num
{
document.write("Num is " arr[num ] "t");
}
document.write(" " "
");
}
var array = [2,32,14,57,6] ;
function borrowArray(from,/*optional*/to)
{
if(!to) to = []; //to = to||[] The effect is the same
for (var element in from) //Use enumeration to traverse the array
to.push(from[element]);//Add elements to the array
return to;//Return
}
var returnnumber = borrowArray(array);//Execute
displayArray(returnnumber);//Print
//Output: Num is 2 Num is 32 Num is 14 Num is 57 Num is 6
5. Variable parameters (Arguments object)
I was stunned when I saw this for the first time. Why do the parameters change? It’s so silly. This argument object is the manager of function parameters. For example, if you have A function defines 3 parameters, then argument has the same data set as the parameters you defined.
Note: the arguments identifier is only valid within the function body. You can also think of it as an attribute of the function!
function checkArgument(x,y,z)
{
if(arguments.length != 3) throw new Error("Parameters do not match"); //Check whether the parameters are legal, very useful!
return x y z;
}
The following example is an example of comparing the size of numbers, and the parameters can be changed.
function compareMaxNumber()
{
var temp = Number.NEGATIVE_INFINITY; //Represents the smallest plural number in javascript
for(var arg=0;arg
{
if(arguments[arg]>temp) temp = arguments[arg];
}
return temp; //Return the largest number in the parameters
}
document.write(compareMaxNumber(2,34,5,23,766,1000) "
");//There can be many parameters here, output 1000
6. Use object properties as parameters
function displayArray(arr) //Print function
{
if(!arr)return;
for(var num =0;num
{
document.write("Num is " arr[num] "t");
}
document.write(" " "
");
}
//
function copyArray(from,from_start,to,to_start,length)//Copy array
{
for(var i = from_start; i {
to.push(from[i]);//Filling
}
return to;
}
//getArray accept Object
function getArray(objarray)
{
//Encapsulate again, call copyArray
return copyArray(objarray.from,objarray.from_start ||0,objarray.to|| [],objarray. to_start ||0,objarray.length);//A little trick is used here.
}
var arr1 = [1,2,3,4,5];
displayArray(getArray({from:arr1,length:4}));
6. Function as data
function add( x,y){return x y;}
function multply(x,y){return x*y;}
function cut(x,y){return x -y;}
operator(operator1, operator2,operator3)//Receives 3 parameters and can use functions as parameters.
{
return operator1(operator2,operator3);//The execution is add();
}
document.write(operator(add,operator(multply,2,4),operator( cut,12,2)));//Output 18
7. Function as a method
The method mentioned here is just to store the function in the attribute of the object. Then called through attributes, the function can be assigned to any variable.
var obj = {};
function display( ) //(something) with parameters
{
return "Love";//something;
}
obj.method = display;//Use direct assignment of objects.
obj.method();//Call. obj.method("Love");
In fact, javascript also references this keyword. Everyone should remember that the object on which the method is called is the value of this. Object.method(); Here Object is the object, and naturally it is the value of this.
var privatename =
{name:"Frank" ,
age:21,
sex:'male',
display:function()
{document.write("my name is:" this.name "t age :" this.age )}};
privatename.display(); //Output name is: Frank age :21
8. Constructor
The constructor initializes the properties of an object , and a constructor used specifically with the new operator. The new operator creates an object, then calls the constructor, passes the newly created object as the value of this, and assigns a value. (To be able to understand)
function createProperty(name,version) //Constructor
{
this.name = name;
this.version = version;
}
var tools = new createProperty("Multply",1.0); Initialization, tools Name, version
9. Define the properties of the function
When you need to use a global immutable value, it will be convenient to use the properties of the Function object (create a namespace later, The attributes of the class are actually pinned on this)
createNamespace.name = "360buy.define";
createNamespace.version = 1.2;
function createNamespace()
{
document.write("Namespace:" createNamespace.name "Version:" createNamespace .version);
}
createNamespace();//Output: Namespace: 360buy.define Version: 1.2
10. Methods apply() and call()
Using these two methods, you can call functions just like calling methods of other objects. The first parameter of both methods is the object to be called, and the following parameters are the function parameters to be called.
function applyCallOperator(args,args2)
{
document.write(args "
");
}
var objpeople = {};
applyCallOperator.apply(objpeople.Frank,[3,4]);// Equivalent to applyCallOperator.call(objpeople.Frank,3,4). There are more in front []
objpeople.Frank();
Summary: This article is just like everyone. Share it here. Next In this article, we implement the javascript class together.