Function and object in JavaScript

Source: Internet
Author: User

Write very well, understand a lot, hereby forward the record

Transferred from:

In JavaScript , all objects inherit from the object prototype, and function acts as the constructor for the objects, so what is the relationship between Funtion and object?

First of all, everything is an object.

1 instanceof // true   2 instanceof Object); // true     3 instanceof Function); // true    4 instanceof Function); // true   

Thus, object inherits itself, Funtion inherits itself, object and function inherit each other, that is to say object and function are both functions and objects. This is a very special point. All functions are objects, but not all objects are functions. The proof is as follows:

1 function foo () {};   2 instanceof // true   3 instanceof // true   4 alert (newinstanceof//  false  

We see that the object instantiated by the constructor of the new + function is not a function, just a subclass of object. Next, we think that all objects have a function constructor stored in the constructor property of the prototype chain, so what is the constructor for object? Prove:

// function function () {[native code]}   // function function () {[native code]}  

Thus we can make sure that object is prototyped by function, and function is prototyped by itself. Function functions are made up of native code, and we don't have to delve into them. There is a function function () {...} Definition, we understand that functions are actually function pointers, as well as function variables. is the equivalent of Foo in function foo () {}. Even if the constructor of object is pointing to function, it can be imagined that functions are a top-level functional definition, much different from custom functions such as function foo () {}.

Look at a statement like this, new function (), instantiate an object as a function, and, as you just object.constructor for Functon function () {}, the new function () The object to be produced should be an object, let's verify:

Alert (newinstanceof//  true  instanceofnew Function ());   false  alert (typeofnew Function ());   function  alert (typeof Object);   function  

In fact, the new function (), will produce an anonymous functions, because function is a top-level function so you can produce a custom function, we can think of all functions as a subclass of function. But all of this, including the subclass of the object, is immutable and is embodied. The result of the TypeOf object illustrates that object is also a function. Keep doing the experiment:

Alert (new//  function Object () {[native code]}  

An understandable doubt. So you can say that anything that can be placed behind new is a function constructor, so object is really like any other function, a pointer to a function or a function variable, but the function is the top-level, so object is constructed by function. As you can understand, function and object One is God, the other is Satan born at the very beginning of the universe, with considerable power. But God is more bright, so high above. object is constructed by function, which is a top-level function. But Satan does not lose the absolute defeat to God, or God will destroy Satan. So or all objects inherit object including function (object and function are both objects and functions). It is equal to all people, including God, who have evil thoughts.

Expand, by object we'll think of these built-in objects such as array,number,string. There are reasons to believe that these are subclasses of object. As follows:

instanceof // true   instanceof // true   instanceof // true   instanceof // false   instanceof // false   instanceof // false  

Of course, they will also have the properties of the object, like the Devil and Satan's relationship, is also a subclass of function, which is constructed by function. Then there is the array,string,number structure of the pair like: new Array (); new number (); The constructor of the new String () is the function Array () {...}; function String () {...}; function number () {...};

instanceof // true   instanceof // true   instanceof // true   // function function () {[native code]}   // function function () {[native code]}   // function function () {[native code]}  

In summary, functions such as built-in functions or objects that are defined as Object,string,array, etc., and custom function keys are subclasses of function. The new function () is equivalent to the Function keyword definition. Here you can elicit, function.prototype all function sharing on the prototype chain, Object.prototype all object sharing on the prototype chain.

Function and object in JavaScript

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: 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.