The relationship between function functions and object objects in JavaScript

Source: Internet
Author: User

Relationship of function object and other internal objects


In addition to the function object, there are many internal objects, such as object, Array, Date, RegExp, Math, and Error. These names actually represent a type, and you can return an object by using the new Operator. The function object, however, differs from other objects in that it returns the string "function" when using typeof to get the type of a function object, and returns the string "object" when typeof an array object or other Object. The following code example typeof different types of situations:

The following is a reference fragment:
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 ()));


Running this code reveals that the preceding 4 statements show "function", while the next 3 statements show "object",The new function is actually returned. This differs greatly from other Objects. The other types array, object, and so on, return a normal object through the new Operator. Although the function itself is an object, it differs from a normal object because it is also an object constructor, that is, you can return an object with a new function, as described Earlier. all objects that typeof return "function" are function objects . Also called such an object as a constructor (constructor)Thusall constructors are objects, but not all objects are Constructors.

Since the function itself is also an object, their type is function, associating to C + +, Java and other object-oriented language class definition, You can guess the function of the function type, that is, you can define some methods and properties of the functional object itself, With the help of the prototype object of the function, it is convenient to modify and extend the definition of the function type, for example, The following extends the functions type, and adds the Method1 method to it, the function is that the popup dialog displays "function":

The following is a reference fragment:
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 invokes the Method1 method that method1 this function object. While it may seem a bit confusing, it's clear to look at the syntax: this is a recursive definition. Because METHOD1 itself is also a function, it also has the properties and methods of the function object, all of which have the recursive nature of the method extensions for the type of functions.

Functions are the basis for all function objects, and object is the basis for all objects, including function Objects. In javascript, any object is an instance of object, so you can modify the type of object to have some common properties and methods for all objects, and modifying the object type is done by prototype:

The following is a reference fragment:
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, which returns the type of the Object. The two alert statements show "object" and "function", respectively.

Passing a function as a parameter

In the previous introduction of the function object essence, each function is represented as a special object, it can be easily assigned to a variable, and then through the variable name for function calls. As a variable, it can be passed as an argument to another function, which has been seen in the previous JavaScript event handling mechanism, such as the following program passing FUNC1 as a parameter to Func2:

The following is a reference fragment:
function Func1 (thefunc) {
Thefunc ();
}
function Func2 () {
Alert ("ok");
}
Func1 (func2);

In the last statement, Func2 is passed as an object to the FUNC1 's formal parameter thefunc, and the THEFUNC call is made internally by func1.in fact, passing a function as a parameter or assigning a function to another variable is the basis of all event Mechanisms.

For example, If you need to do some initialization work on page loading, you can define an init function for initialization and then bind it to a page load-completed event by Window.onload=init. The init here is a function object that can be added to the OnLoad event list of WINDOW. ————————————————————————————————————————————————————————————————————————————

function is the most common concept in javascript, and the function in JavaScript is one of the easiest to start with, but it is also the most difficult concept for JavaScript to Understand.

Today we're going to try to understand function and object. because there are some people who may get confused in the early days. what is the relationship between Them. of course, no exception to the original I.

Note: official definition: in javascript, each function is actually a function object.

Let's take a look at the simplest two code, which is the easiest to understand.

?
123456 < Code class= "js keyword" >function fn () {} var Code class= "js plain" >obj = {} console.log (fn instanceof function) //true console.log (obj instanceof Object) //true console.log (fn instanceof object) //true console.log (obj instanceof Function) //false

The previous two printing effect, everyone is easy to understand. the back FN instanceof object is True. here too, from the definition of function: in JavaScript all functions are actually function objects. So it's not strange to be True. obj instanceof function is false, of course not. because he is an object, not a Function.

Let's see one more Code.

?
12 console.log(Function instanceofObject); // trueconsole.log(Object instanceofFunction); // true

The code is SIMPLE. run structure two is true, why? The first is the definition of a function, (in javascript, the function is actually a function object), of course, true, What about the second? the object is also a function?

Object is also a function. because the structure of object is the functions object () {native code}.

This form, it is clear that the declaration of an object function, of course, is the function, so two is True.

Their two functions and the object function implement the code, which of course is different. how they do it, then we don't go into detail, if you want to think about, you can understand the browser knowledge.

The relationship between function functions and object objects in 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.