The relationship between function and object in JavaScript

Source: Internet
Author: User

Today, QQ technology discussion group inside there is a person chatting, said today's interview, the interviewer asked his first question he was confused, said is the relationship between function and object, or what difference they have. At that time I smiled, thinking that the simple question you are not clear, then you still have the confidence to go to the interview. However, when I tried to organize the language to taunt him a few words, I found that I also organization is not clear. A bitter lesson, step by step to repeat the relationship between them.

First of all, function is a constructor, it must have its corresponding prototype object Function.prototype, we use the console printing can draw Function.prototype=function () {}. Because the console printing code is very simple, directly function.prototype a word, so do not write code, to the last together on the line.

Second, the function itself is an object, so it must have its corresponding constructor function.constructor, also using the console to print out that Function.constructor is the function itself. Similarly, function will have the corresponding prototype object function.__proto__, through the console printing, we can draw Function.__proto__=function () {}, Because Function.prototype is also equal to Function () {}, so function.prototype==function.__proto__.

Since Function.prototype=function () {},function () {} is an empty function, it will have a corresponding constructor function.prototype.constructor= function itself. The function () {} also has a prototype object function.prototype.__proto__ = object{}.

Next we look at object, which is also the constructor and prototype object object.prototype=object{}. The object itself is also a object.constructor, so it has the corresponding constructor, and its constructor object.constructor=function the constructor function. From the above, Function.constructor is also equal to the function constructor, so we can get the conclusion object.constructor==function.constructor, and are all function constructors. For this phenomenon, we can understand that a function can instantiate object objects or instantiate a function object. The diagram between them, as shown:

  

Through their intricate relationships above, or not accurately draw them who are the instance objects, that I can get through the Instancsof keyword that they are each other instance objects. So the relationship between function and object is summed up as: 1, they are instance objects to each other. 2, function can instantiate object objects can also instantiate function objects.

PS: Console Print Results:

  

  

  

Relationship of function and object 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.