JavaScript Advanced Function (iv) _ Basics

Source: Internet
Author: User
1, the creation of the function

Since I have used a lot of functions before, I am not here to share more.

2. Nested functions
Copy Code code as follows:

function of the ()
{
function people ()//nested functions, only the
{
document.write ("My wish is to be Du Fu, recently online his comics, he live Well chic");
}
People ();
}

3, the direct amount of the function

JavaScript allows a function to be defined by a direct amount, which is simply an expression, an anonymous function. Details: Anonymous functions will be used later, to understand!
Copy Code code as follows:

Function Country (people)//functions Declaration
{
return peole;
}
var fun = function (people) {return people;};/ /through an expression declaration. Effect equal

4, the function of the parameters (here we should note)

When you call a function with a parameter less than the number of arguments that are declared, the other parameter is a undefined value.
Copy Code code as follows:

Print function
function Displayarray (arr)
{
if (!arr) return;
for (Var num =0;num<arr.length;num++)
{
document.write ("Num is" + arr[num]+ "\ t");
}
document.write ("" + "<br/>");
}
var array = [2,32,14,57,6];
function Borrowarray (from,/*optional*/to)
{
if (!to) to = []; to = to| | [] the effect is equivalent
for (var element in from)//using enumerations to traverse an array
To.push (From[element]);//Add elements to array
Return to;//
}
var returnnumber = Borrowarray (array);//Execute
Displayarray (Returnnumber);//Print
Output: num is 2 num is num # is num # is 6

5, variable parameters (Arguments object)
See here for the first time I also faint, how the parameter also changes, how white, this argument object is the function parameter manager, for example you have a function, defined 3 parameters, that argument has a and you define the parameter, the same dataset.
Note that the arguments identifier is valid in the body of the function. You can also take it as a function of the property bar!
Copy Code code as follows:

function Checkargument (x,y,z)
{
if (arguments.length!= 3) throw new Error ("Parameter mismatch"); Check the parameters are legitimate, very useful!
return x+y+z;
}

The following example is an example of the size of a number, and the parameters can be changed.
Copy Code code as follows:

function Comparemaxnumber ()
{
var temp = number.negative_infinity;//Represents the smallest number of JavaScript numbers
for (Var arg=0;arg<arguments.length;arg++)
{
if (arguments[arg]>temp) temp = Arguments[arg];
}
return temp; Returns the largest number in a parameter
}
document.write (Comparemaxnumber (2,34,5,23,766,1000) + "<br/>");//There can be a lot of parameters here, output 1000

6, the object properties as parameters
Copy Code code as follows:

function Displayarray (arr)//print functions
{
if (!arr) return;
for (Var num =0;num<arr.length;num++)
{
document.write ("Num is" + arr[num]+ "\ t");
}
document.write ("" + "<br/>");
}
//
function Copyarray (from,from_start,to,to_start,length)//copy Array
{
for (var i = From_start i <length;i++)
{
To.push (From[i]);//Fill
}
return to;
}
GetArray Accept Object
function GetArray (Objarray)
{
Encapsulation again, call Copyarray
Return Copyarray (Objarray.from,objarray.from_start | | 0,objarray.to| | [],objarray.to_start | | 0,OBJARRAY.LENGTH);//There is a little skill here.
}
var arr1 = [1,2,3,4,5];
Displayarray (GetArray ({from:arr1,length:4}));

6, as the data function
Copy Code code as follows:

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)//Receive 3 parameters, you can use the function as a parameter.
{
Return Operator1 (OPERATOR2,OPERATOR3);//execution is add ();
}
document.write (operator) (Add,operator (multply,2,4), operator (cut,12,2));//Output 18.

7, as a function of the method

The method here is simply to store the function in the object's properties and then call it through the property, which can be assigned to any variable.
Copy Code code as follows:

var obj = {};
function display ()//(something) with parameters
{
Return to "Love";//something;
}
Obj.method = display;//uses the direct amount of the object to assign a value.
Obj.method ()//call. Obj.method ("Love");

In fact, JavaScript also references the This keyword. Remember that the object that calls the method is actually the value of this. Object.Method (); This is the object, and naturally it is the value of this.
Copy Code code as follows:

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 function

A constructor is a property that initializes an object and a constructor that is used with the new operator, creates an object, and then calls the constructor, passing the newly created object as this value, and assigning the value. (To be able to understand)
Copy Code code as follows:

function CreateProperty (name,version)//constructor
{
THIS.name = name;
This.version = version;
}
var tools = new CreateProperty ("Multply", 1.0); initialization, tool name, version

9, define the properties of the function

When you need to use a global invariant value, it is convenient to use the properties of the function object (create the namespace later, the class's properties are actually pinned to this)
Copy Code code as follows:

Createnamespace.name = "360buy.define";
Createnamespace.version = 1.2;
function Createnamespace ()
{
document.write ("namespace:" +createnamespace.name + "version:" + createnamespace.version);
}
Createnamespace ()//output: namespaces: 360buy.define Version: 1.2

10. Method apply () and call ()

Using these two methods, you can call a function just like a method of another object, two methods the first parameter is the object to invoke, and the following argument is the function parameter to call.
Copy Code code as follows:

function Applycalloperator (ARGS,ARGS2)
{
document.write (args+ "<br/>");
}
var objpeople = {};
Applycalloperator.apply (objpeople. frank,[3,4]);//equal to Applycalloperator.call (objpeople. frank,3,4) front more []
Objpeople. Frank ();

Summary: This article is like everybody shares to come here, next article we implement JavaScript class together.
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.