JavaScript advanced functions (4) _ basic knowledge

Source: Internet
Author: User
Tags define function
In this section, I will share with you how to create functions, nested functions, data functions, object functions, and so on.

Since many functions have been used previously, I will not share them here.

2. nested Functions

The Code is as follows:


Function china ()
{
Function people () // nested function, which is used only by china
{
Document. write ("My wish is to be Du Fu, his cartoon on the Internet recently. He lives well ");
}
People ();
}


3. Direct amount of functions

Javascript allows functions to be defined by quantity. To put it bluntly, it is an expression (anonymous function ). Details: anonymous functions will be used in the future. Please understand!

The Code is as follows:


Function china (people) // function declaration
{
Return peole;
}
Var fun = function (people) {return people;}; // declare by expression. Equivalent Effect


4. function parameters (please note)

When the number of parameters you call a function is less than the number of declared parameters, other parameters are undefined values.

The Code is as follows:


// Print the 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 */)
{
If (! To) to = []; // to = to | [] the effect is equivalent.
For (var element in from) // use enumeration to traverse the Array
To. push (from [element]); // Add the element 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 am also dizzy when I see it for the first time. How can I change the parameters? It's more white. This argument object is the manager of function parameters. For example, if you have a function that defines three parameters, the argument has a dataset that is the same as the parameter you have defined.
Note: The arguments identifier is valid in the function body. You can also regard it as a function attribute!

The Code is as follows:


Function checkArgument (x, y, z)
{
If (arguments. length! = 3) throw new Error ("parameter mismatch"); // check whether the parameter is valid. It is quite helpful!
Return x + y + z;
}


The following is an example of comparing the numbers and the parameters can be changed.

The Code is as follows:


Function compareMaxNumber ()
{
Var temp = Number. NEGATIVE_INFINITY; // represents the smallest complex Number in javascript.
For (var arg = 0; arg {
If (arguments [arg]> temp) temp = arguments [arg];
}
Return temp; // The maximum number in the returned parameter.
}
Document. write (compareMaxNumber (,) +"
"); // There can be many parameters here, with an output of 1000


6. Using object attributes as parameters

The Code is as follows:


Function displayArray (arr) // print the 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 an array
{
For (var I = from_start; I {
To. push (from [I]); // fill
}
Return;
}
// GetArray accept object
Function getArray (objarray)
{
// Re-encapsulate and call copyArray
Return copyArray (objarray. from, objarray. from_start | 0, objarray. to | [], objarray. to_start | 0, objarray. length); // some tips are used here.
}
Var arr1 = [1, 2, 4, 5];
DisplayArray (getArray ({from: arr1, length: 4 }));


6. functions as data

The Code is 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) // receives three parameters and can be used as parameters.
{
Return operator1 (operator2, operator3); // add () is executed ();
}
Document. write (operator (add, operator (multply, 2, 4), operator (cut, 12, 2); // output 18


7. functions used as methods

The method mentioned here is to store the function in the attributes of the object and call it through the attribute. The function can be assigned to any variable.

The Code is as follows:


Var obj = {};
Function display () // (something) with Parameters
{
Return "Love"; // something;
}
Obj. method = display; // assign values using the direct quantity of objects.
Obj. method (); // call. Obj. method ("Love ");


In fact, javascript also references the this keyword. Remember that the object that calls the method is the value of this. Object. method (); here the Object is the Object, which is the value of this.

The Code is 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

Constructor is a constructor that initializes the attributes of an object and is used with the new operator. The new operator creates an object and then calls the constructor, pass the new object as the value of this and assign a value. (To be understandable)

The Code is 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 Function Attributes

When you need to use a global constant value, it is very convenient to use the Function object attributes (in the future, the attributes of namespaces and classes will be placed on this)

The Code is as follows:


CreateNamespace. name = "360buy. define ";
CreateNamespace. version = 1.2;
Function createNamespace ()
{
Document. write ("namespace:" + createNamespace. name + "version:" + createNamespace. version );
}
CreateNamespace (); // output: namespace: 360buy. define release: 1.2


10. apply () and call () Methods ()

Using these two methods can call functions like calling methods of other objects. The first parameter of both methods is the object to be called, and the following parameter is the function parameter to be called.

The Code is as follows:


Function applyCallOperator (args, args2)
{
Document. write (args +"
");
}
Var objpeople = {};
ApplyCallOperator. apply (objpeople. Frank, [3, 4]); // equals applyCallOperator. call (objpeople. Frank, 3, 4) More front []
Objpeople. Frank ();


Summary: This article is just like sharing it here. Next we will implement javascript classes 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.