Deep understanding of JavaScript prototypes and Closures (2)--relationships between functions and objects

Source: Internet
Author: User

Reproduced in http://www.cnblogs.com/wangfupeng1988/p/3978035.html. Thank you!

The above (understanding JavaScript prototypes and Scope series (1)-Everything is an object) has already been mentioned, and the function is one of the objects because it can be judged by the instanceof function.

var fn = function () {};console.log (fn instanceof Object);  True

Right! A function is an object, but a function is not like an array-you can say that an array is one of the objects because the array is like a subset of objects. But between the function and the object, but not just a contained and contained relationship, the relationship between functions and objects is more complex, and even a little chicken eggs to raw chicken logic, we have a wisp of this section.

Let's look at a small example.

        function Fn () {            this.name = ' Wang Fu ';            This.year = 1988;        }        var fn1 = new Fn ();

The above example is simple enough to illustrate that an object can be created by a function. Right! Can only illustrate this point.

But I would say-- objects are created through functions --Some may argue: No! Because:

var obj = {a:10, b:20};var arr = [5, ' X ', true];

But sorry, this--really--is a--"shortcut", in the programming language, is generally called "syntax sugar."

Do "Grammar sugar" do the best is Microsoft eldest brother, it put their home C # that kid did not male and female from, this want to figure personally see people love, who chengxiang also get place with people explain-actually it is a boy!

In fact, the essence of the above code is:

        var obj = {a:10, b:20};        var arr = [5, ' X ', true];        var obj = new Object ();        OBJ.A = ten;        OBJ.B =;        var arr = new Array ();        Arr[0] = 5;        ARR[1] = ' x ';        ARR[2] = true;

And the Object and Array are functions:

Console.log (typeof (Object));  Functionconsole.log (typeof (Array));  function

Therefore, it is very responsible to say that objects are created through functions.

Now is not confused--the object is created by the function, but the function is an object--God! What is the relationship between functions and objects?

Don't worry! To uncover this mystery, you'll have to get to know another old friend--prototype prototype.

This series of articles is not intended to be a lengthy thousands of words, we run small steps, not too boring to see.

Deep understanding of JavaScript prototypes and Closures (2)--relationships between functions and objects

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.