Javascript Judgment function type Perfect solution _javascript tips

Source: Internet
Author: User
Tags jquery library
However, there are some details that are not well known to us. John Resig, after analyzing these details, provides us with a perfect solution, which is described in detail in this article:

The details of the traditional methods not known
There is no doubt that when judging the type of a function, we are using the TypeOf method, such as:
Copy Code code as follows:

function fn () {
Content
}
Alert (typeof fn)//result is "function".

However, the method does not work as we think in some browsers.

1, Firefox2 and Firefox3
In both browsers, the type of HTML object element is detected with typeof, resulting in an imprecise "function" result rather than "object" such as HTMLDocument. Such as:
Copy Code code as follows:

Alert (typeof HTMLDocument);
In Firefox2, the result is "function";
In Firefox3, the result is "object";

2, Firefox2
For regular expressions, the result returned in the browser is "function" (the result in Firefox3 is "object"), such as:
Copy Code code as follows:

var reg =/test/;
Alert (typeof Reg);
In Firefox2, the result is "function";
In Firefox3, the result is "object";

Note: I tested in Safari and the result is "function".
3, IE6 and IE7
Using the TypeOf method for DOM elements in IE, the result is "object". Such as:
Copy Code code as follows:

Alert (typeof document.getElementsByTagName ("Body") [0].getattribute);
The result is "object"

4, Safari 3
Safari thinks that the nodelist of DOM elements is a function, such as:
Copy Code code as follows:

Alert (typeof Document.body.childNodes);
The result is "function."

Obviously, if you want to test whether an object is a function, using the TypeOf method does not guarantee the test results in real sense. Then we need a solution that guarantees test results in all browsers. We know that the function itself has both the apply () and call () methods, but the two methods do not exist in IE with a problem, try the following test:
Copy Code code as follows:

Alert (typeof document.getElementsByTagName ("Body") [0].getattribute.call)
In IE, the result is "undefined"

Obviously, we cannot take advantage of these two methods.

Second, the perfect solution and implementation process
John Resig provides us with a perfect solution, a complex but very stable way to determine whether an object is a function is as follows:
Copy Code code as follows:

function Isfunction (FN) {
Return!! fn &&!fn.nodename && fn.constructor!= String &&
Fn.constructor!= RegExp && fn.constructor!= Array &&
/function/i.test (fn + "");
}

This function first guarantees that the test object exists and serializes it into a string containing a "function", which is the basis of our detection (fn.constructor!= string,fn.constructor!= Array, and Fn.constructor!= RegExp). In addition, we need to ensure that the declared function is not a DOM node (fn.nodename). Then we can do the ToString test. If we convert a function to a string, in a browser (fn+ "") gives us the result like this "function name () {...}". Now, it's simple to judge whether it's a function, just to determine if the string contains the word "function." This is amazing, and for any problematic function, we can get the results we need in all browsers. This function is somewhat unsatisfactory compared to traditional methods, and the authors suggest that we use it conservatively.

John Resig, a developer of the jquery library, believes that friends who use the library are not unfamiliar with the library's concise syntax and excellent performance. In addition to the pursuit of code simplicity and performance efficiency, its perfect spirit is also impressive. If you are a perfectionist, I believe this article is very helpful to you.
Related Article

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.