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