JavaScript functions Internal properties and Function methods examples of _javascript skills

Source: Internet
Author: User

A function is an event-driven or reusable block of code that executes when it is invoked.

A function is an object that has its own properties and methods. First of all, through the console output function attribute method to visually look at:

Function internal properties include only two special objects: arguments and this.

Function properties include: Length and prototype

Functional methods (not inherited) include: Apply () and call ()

Inherited function Methods: Bind (), toString (), tolocalestring (), valueof ()

The others are not ripe at the moment, and are later added

1. Function Internal Properties

Inside the function, there are two special objects, arguments and this.

Arguments Property

Arguments is a class array object that contains all the arguments for the passed function, and arguments's main purpose is to save the function arguments, but this object has a Callee property, which is a pointer to the function that owns the arguments object. The following is a very classical factorial function.

function factorial (num) {
if (num <= 1) {return
1;
} else{return
num * factorial (num-1);
}

The definition of a factorial function typically uses a recursive algorithm, as shown in the code above, which is fine when there is a function name and the function name does not change. However, the execution of this function is tightly coupled to the factorial name, and the Arguments.callee can be used to eliminate this tight coupling phenomenon (such as the change of function name).

function factorial (num) {
if (num<=1) {return
1;
} else{return
num * Arguments.callee (NUM-1);
}
}

The function body of the factorial () function is overridden, and no further reference is factorial to the functional name. In this way, even if the function name is changed, the recursive call is guaranteed to complete normally. For example:

var truefactorial = factorial; Change the pointer (save position) of the original function body
factorial = function () {//factorial point to the new function returning 0 return
0;
}
Alert (Truefactorial (5));
Alert (factorial (5));//0

If the Arguments.callee is not used, then truefactorial (5) also returns 0;

This property

2. Method of function

Each function contains two methods that are not inherited: Apply () and call (). The purpose of both methods is to call a function in a particular domain (see the wood here to understand); its real strength is in being able to expand the scope in which the function is run

About JavaScript function Internal properties and function methods of knowledge, small series to introduce so many people, I hope to help you!

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.