Understanding Javascript_08 _ function object

Source: Internet
Author: User

Function object
First, you must clarify the concept that a function is an object, and an object that represents a function is a function object. Since it is an object, who constructed it? Next let's look at a description: when defining a Function in JavaScript code or calling a Function to create a Function, the Function will eventually be called in a similar way: var newFun = Function (funArgs, funBody );. We can see that the Function object is constructed by the Function object.
Note: A Function object is also a Function object. For more information about functions, see the following blog.
Let's look at a piece of code:
Copy codeThe Code is as follows:
// Define method 1
Function func (x ){
Alert (x );
}
// Method 2
Var func = function (x ){
Alert (x );
};
// Actual execution
Var func = new Function ("x", "alert (x );");

The code above shows that the Function func is constructed after the Function object receives two parameters!

NOTE: For the differences between definition method 1 and definition method 2, see the subsequent blog



Function object creation process

To create a function object, follow these steps:

1. Create a build-in object fn

2. Set the internal [[Prototype] of fn to Function. prototype.
3. Set the internal [Call] attribute. It is an internal implementation method to process the function Call logic. (Simple understanding is to call the function body)

4. Set the internal [Construct] attribute, which is an internal implementation method for processing the creation process of logical reference objects. (It is simply understood as the document "Understanding the creation process of objects)

5. Set fn. length to funArgs. length. If the function does not have a parameter, set fn. length to 0.
6. Use the same logic of new Object () to create an Object fnProto
7. Set fnProto. constructor to fn
8. Set fn. prototype to fnProto
9. Return fn

The difference between Step 1 and Step 6 is that step 1 only creates the internal data structure (build-in Object structure) for implementing the object Object and completes the necessary internal initialization work, however, its [[Prototype], [Call], [[Construct] and other attributes should be null or internal initialization values, that is, we can understand that it does not point to any object (for attributes such as [[Prototype ), or does not contain any processing (for methods such as [[Call] and [[Construct ). Step 6: Create a new object according to "Understanding the creation process of objects", and set its [[Prototype.
From the above process, we can understand that a function is defined at any time and its prototype is an Object instance. By default, when we create an instance Object of a UDF, their Prototype chain will point to the Object. prototype.

Note: A special feature of a Function is that its [[Call] and [Construct] process logic are the same. The deep-seated reasons will be described in the follow-up blog.

Below we will write some example scripts to test the above theory:

Copy codeThe Code is as follows:
Function Animal (){
}
Alert (Animal. length); // 0

Var dog = new Animal ();

This JS proves the correctness of Step 5. Finally, let's take a look at the memory diagram of the function object. For simplicity, the memory diagram only describes the construction process of Animal:

From an overall analysis graph:

The image itself can already explain a lot of problems. Let's take a look at the previous instanceof principles, Object Construction Principles, and prototype chain principles. I will not talk much about it.

In fact, the Function Object is a wonderful Object, and its relationship with the Object is even more confusing. I will explain it all in "Understanding Javascript_09_Function and Object.


The final statement: the theory is too complicated and I will not change it to ensure its correctness. However, after multiple tests, no conflict between theory and practice has been found.

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.