The relationship between JavaScript prototypes and scope (2) Functions and objects

Source: Internet
Author: User
Tags log

As mentioned above (understanding the JavaScript prototype and scope series (1)-Everything is an object), a function is a kind of object 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 a kind of object, because an array is like a subset of objects. But the function and the object, but not just a kind of inclusion and inclusion of the relationship between the function and the object of the relationship between the complex, and even a bit of chicken eggs raw chicken logic, we this section is a wisp of wisp.

Let's look at a small example first.

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

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

But I would say--objects are created by functions--some may refute: no! Because:

var obj = {a:10, b:20};

var arr = [5, ' X ', true];

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

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

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

Words return to the story--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)); function

Console.log (typeof (Array)); function

So, you can say in person--objects are created by functions.

Now it's not confusing--objects are created by functions, and functions are objects--oh, God! What is the relationship between a function and an object?

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

This series of articles is not intended to be a thousands of-word long speech, we small steps to run, not to see too boring.

Author: cnblogs Wang Fu

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.