JavaScript functions Internal properties and Function methods

Source: Internet
Author: User

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

    • The function's internal properties include only two special objects: arguments and this.
    • Function properties include: Length and prototype
    • function methods (non-inheritance) include:apply () and call ()
    • Inherited function Methods: Bind (), toString (), tolocalestring (), ValueOf ()
    • The others are not ripe at the moment, and the back is replenished
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 parameters of the passed-in function, and the main purpose of arguments 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 , here is the very classic factorial function.

function factorial (num){    if1){        return1;    else{        return num * factorial(num-1);      }}

A recursive algorithm is commonly used to define factorial functions, 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 with the name factorial, and in order to eliminate this close coupling phenomenon ( such as change of function name ), Arguments.callee can be used.

function factorial(num){    if(num<=1){        return1;    else{        returnarguments.callee(num-1);    }}

the factorial () function is overridden by the function body, and no further reference to the name factorial. This way, even if you change the function name, you can ensure that recursive calls are completed normally. For example:

var trueFactorial = factorial;  //改变原函数体的指针(保存位置)function (){//factorial 指向返回0的新函数    return0;}alert(trueFactorial(5));    //120alert(factorial(5));        //0

If you do not use Arguments.callee, then truefactorial (5) also returns 0;

This property is 2. Methods of the function

Each function contains two non-inherited methods: Apply () and call (). The purpose of both methods is to invoke the function in a particular domain (see the wood here); its real strength is the ability to extend the scope of functions on which they run

Keep writing tomorrow.

JavaScript functions Internal properties and Function methods

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.