Say in the building pig understanding and practice ability still lack of heat, in this article has copied the Li Shi teacher a lot of things written in his blog as a "knowledge reserve." This time is still not exception. In the 5th chapter, the opening of the second paragraph was deeply attracted and impressed: "The function has all the characteristics of the object, you can completely call the function when the object." In fact, the function is the object, but more than the general object of a bracket "{}" operator, this operator is used to perform the logic of the function, that is, the function itself can also be invoked, the general object can not be called, in addition to the exact same. A few words, but a profound interpretation of the relationship between objects and functions. The following is a few simple pieces of code written by the pig, to demonstrate the JavaScript built-in object and function relationships.
1, function is Object,object is function
Copy Code code as follows:
Alert (Function instanceof Object); True
Alert (Object instanceof Function); True
As you can see, through the instanceof operator, the function is the object, and the object is the function.
2, since 1 is set up, then function extension of the prototype method, object can be "get"?
Copy Code code 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, we have realized the magic "without pains" for the prototype method of function extension funcmethod,object.
3, since 1 and 2 are all set up, then the object extension of the prototype method, function can "get" it?!
Code
Copy Code code 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?
Does the line with the last question mark in the above code pop true or False? Sell a case, according to the flat pig straight and simple expression, you should already know the result, here do not release the answer.
Finally, the House pig in the pride of narcissism here: Personally think the above three pieces of code should be compared to the original book to verify that "function is the essence of the object" code more convincing.