The relationship of functions (function) in JavaScript to Objects (object)

Source: Internet
Author: User

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.


function fn () {}

var 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


front two printing effect, everyone is easy to understand. 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 surprising. Because he is an object, not a function.


Let's see one more code.


console.log (Function instanceof  object); True

console.log (Object instanceof Function);  //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.


I hope you can understand the relationship between function and object, do not understand the public message or add Group: 186659233 Ask Us


This article belongs to Wu Shi Wei's blog, the public number: BIANCHENGDEREN,QQ Group: 186659233 original articles, reproduced when please indicate the source and corresponding links: http://www.wutongwei.com/front/infor_ showone.tweb?id=160, welcome everyone to spread and share.

This article is from the "Ophir Technology Research" blog, please be sure to keep this source http://ophir.blog.51cto.com/1741965/1725479

The relationship of functions (function) in JavaScript to Objects (object)

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.