Three simple code-javascript skills for verifying the relationship between objects and functions in javascript

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

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.