Review classic books today. This time I read the & lt; JavaScript & gt; written by Miss Li Zhan of the blog Park, which is also one of the most frequently viewed technical books by Lou pig. When the understanding and practical ability of Lou pig are still insufficient, I copied a lot of things written by Li Zhan in my blog as a "knowledge reserve ". This time is still not vulgar. When turning to Chapter 2, I was deeply attracted and impressed by the second paragraph of the opening: "A function has all the features of an object. You can call a function as an object. In fact, a function is an object, but a bracket "{}" operator is added to an ordinary object. This operator is used to execute the function logic, that is, the function itself can be called, generally, objects cannot be called, but they are identical ". The relationship between objects and functions is explained in a few words. The following code demonstrates the relationship between javascript built-in objects and functions.
1. A Function is an Object, and an Object is a Function.
The Code is as follows:
Alert (Function instanceof Object); // true
Alert (Object instanceof Function); // true
As you can see, through the instanceof operator, a function is an object and an object is a function.
2. Since 1 is true, can an Object "get" The Function Extension's prototype method?
The Code is as follows:
Alert (Object. funcMethod); // undefined
Function. prototype. funcMethod = function (){
/* Some function method code here */
}
Alert (Function. funcMethod );
Alert (Object. funcMethod );
Alert (Function. funcMethod === Object. funcMethod); // true
You are not mistaken. For the Function Extension Prototype Method funcMethod, the Object implements the magic "gain nothing ".
3. Since both 1 and 2 are true, can the Function "get" The prototype method of Object extension be used ?!
Code
The Code is as follows:
Alert (Function. objMethod); // undefined
Object. prototype. objMethod = function (){
/* Some object method code here */
}
Alert (Object. objMethod );
Alert (Function. objMethod );
Alert (Function. objMethod === Object. objMethod); // true or false?
In the above Code, is the row with the most question mark "true" or "false" displayed? Sell a Guan Zi. According to Lou pig's straightforward and simple expression, you should already know the result. I will not announce the answer here.
Finally, ronglou pig is proud to be narcissistic here: I personally think the above three pieces of code should be more convincing than the code in the original book that "function is the essence of objects.