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