The function type of javascript

Source: Internet
Author: User

One, function definition

A function is actually an object that has properties and methods as well as other reference types. Functions can be defined in three ways, namely, function declaration syntax definitions, function expression definitions, and functions constructor definitions.

1. function declaration Syntax definition

function functionname (value1 ...) {    // function Body }

2. Function expression definition

var function (value1 ...) {    // function Body }

3.Function constructor Definition

By creating a function from the functions constructor, you can pass in any number of arguments to the constructor, but it is worth noting that the last parameter passed in is used as the body of the function , while the other parameters are passed into the function as arguments. It is deprecated to use this method to define a function because it causes parsing of two of code, parsing the regular ECMAScript code for the first time, and parsing the string passed into the constructor for the second time, affecting performance.

var New Function ("value",..., "functional Body");

  Note: A function is an introduction to a value type, so the function name is simply a pointer to a function, and when the function name is used to assign a value to another variable name, only a pointer is copied. That is, when the following a is set to NULL, only a saved pointer is eliminated, and the B call function is not affected.

var function (value1) {    returnnull; b (1);

Second, overloading of functions

The function itself is not overloaded, because in JavaScript, a function can receive any parameter, so the function overload does not occur because of the number of arguments. However, the function overloading can be implemented by special notation.

Idea: Using the function internal attribute arguments.length to judge, carries on the different processing which passes the different parameter, thus realizes the function overloading.

The difference between function declaration and function expression

The parser does not parse the two defined function methods. The parser takes precedence over function-declared functions so that they are available before the code executes (the function declaration is advanced). The function expression is parsed when it executes to that line of code.

Four, closed package
Interview questions: Sort the objects in the array by an attribute.

Idea: Use the array's sort () to receive a contrast function, which is the return value of another function.

var arr = [{name: "Lyf", Age:20},{name: "Gulu", Age:18}]; function Namesort (name) {    returnfunction(obj1,obj2) {        return obj1[ name]-obj2[name];}    } Arr.sort (Namesort ("Age"));

V. Function Interior Properties

1.arguments: A pseudo-array containing all the parameters of the function.

Its Arguments.callee property points to the function that contains the arguments object. This property can be used for function calls of recursive functions.

Example: The notation of a factorial function.

function factorial (num) {    if(num<=1) {        return 1;    } Else {        return Num*arguments.callee (num-1);    }} Benefit: Low Coupling

2.this: Environment variable for function execution.

3.caller: Call the function reference of the current function.

Cases:

function A () {    console.log (a.caller); // Point to the B that calls a }function  B () {    A ();} b ();

Vi. properties and methods of functions

1.length: This attribute refers to the number of parameters that need to be passed in when defining a function. Use as: function name. length;

2.prototype: Prototype function.

Both 3.apply and call: Methods change the Environment object that invokes the function, in short, change the This value of the function. There is no difference between the two ways of passing in parameters. Apply at the time of the parameter, you can pass in an array or a pseudo-array arguments,call is to list the arguments in sequence to the incoming function, see the following syntax.

Apply (Environment object, [value1,...]) or apply (Environment object, arguments).

Call (Environment object, value1,value2 ...)

4.bind ()

Creates a function instance whose this value is bound to the value passed to the bind () function.

Window.age =; var lizi = {age:18}; function Sayage () {    alert (this. age);} var say = sayage.bind (Lizi); say (); //  -

The function type of javascript

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.