Understanding JavaScript Functions (differences and relationships between functions and objects)

Source: Internet
Author: User

Original article: http://harrison2010.javaeye.com/blog/173446

Relationship Between Function objects and other internal objects

 

In addition to function objects, there are also many internal objects, such as object, array, date, Regexp, math, and error. These names actually represent a type, and an object can be returned through the new operator. However, function objects are different from other objects. When typeof is used to obtain the type of a function object, it still returns the string "function", while typeof is an array object or another object, it returns the string "object ". The followingCodeFor different types of typeof:

The following is a reference clip:

 
Alert (typeof (function); alert (typeof (new function (); alert (typeof (array); alert (typeof (object )); alert (typeof (new array (); alert (typeof (new date (); alert (typeof (new object ()));

After running this code, we can find that all the first four statements show "function", and the last three statements show "object". It can be seen that a new function actually returns a function. This is quite different from other objects. Other types of arrays and objects will return a common object through the new operator. Although a function is also an object, it is different from a common object because it is also an object constructor. That is to say, you can use a new function to return an object, this is already described earlier. All typeof objects that return "function" are function objects. This is also called constructor. Therefore, all constructors are objects, but not all objects are constructors.

Since functions are also an object, their types are functions, and the class definitions of C ++, Java, and other object-oriented languages can be inferred to the role of function types, that is, some methods and attributes can be defined for the function object itself. With the help of the prototype object of the function, you can easily modify and expand the definition of the function type. For example, the following extends the function type function, the Method1 method is added for it. The pop-up dialog box displays "function ":

The following is a reference clip:

 
Function. prototype. method1 = function () {alert ("function");} function func1 (a, B, c) {return A + B + C;} func1.method1 (); func1.method1. method1 ();

Note the last statement: func1.method1. mehotd1 (), which calls the Method1 method of the Method1 function object. Although it may seem confusing, the syntax is clear: this is a recursive definition. Because Method1 is also a function, it also has the attributes and methods of function objects. All method extensions of function types have such recursive properties.

Function is the basis of all function objects, while object is the basis of all objects (including function objects. In JavaScript, any object is an object instance. Therefore, you can modify the object type to make all objects have some common attributes and methods, you can modify the object type through prototype:

The following is a reference clip:

Object. prototype. getType = function () {return typeof (this);} var array1 = new array (); function func1 (a, B) {return a + B ;} alert (array1.gettype (); alert (func1.gettype ());

The above Code adds the GetType method to all objects to return the object type. The two alert statements show "object" and "function" respectively ".

Passing functions as parameters

We have already introduced the essence of function objects. Each function is represented as a special object, which can be conveniently assigned to a variable and then called by the variable name. As a variable, it can be passed to another function in the form of parameters, which has been seen in the previous section of the Javascript event processing mechanism, such as the followingProgramPass func1 as a parameter to func2:

The following is a reference clip:

 
Function func1 (thefunc) {thefunc ();} function func2 () {alert ("OK") ;}func1 (func2 );

In the last statement, func2 is passed to thefunc as an object, and then thefunc is called internally by func1. In fact, passing functions as parameters or assigning functions to other variables is the basis of all event mechanisms.

for example, if you need to perform initialization during page loading, You can first define an init initialization function and then use window. onload = Init; the statement binds it to the event loaded on the page. Init is a function object, which can be added to the onload event list of window.

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.