Three-piece simple code _javascript tips for verifying the relationship between object and function in JavaScript

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.