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
The previous two printing effect, everyone is easy to understand. The 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. 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.
The relationship of functions (function) in JavaScript to Objects (object)