Bug fixes for isfunction methods in jquery _jquery

Source: Internet
Author: User

jQuery 1.4 Source 449 line (Core.js 431 lines), the method to determine whether the function is as follows (the idea comes from Douglas Crockford 's "The Miller Device"):

Isfunction: function(  obj  )   {
return toString. call(obj) = = "[Object Function]" ;
},

The author of JQuery also makes a partial comment:

Test/unit/core.js for details concerning isfunction. Since version 1.3, DOM methods and functions like alert aren ' t supported. They return False on IE (#2968).

That is, this method does not correctly recognize the DOM method and some functions (such as alert method, etc.) under IE.

Why is that?

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> repair jQuery isfunction method bug</title> </pead> <body> <p> Use the typeof operator to detect various methods:</p> <script> Document.writeln (' typeof eval: ' + typeof eval + ' <br> '); Document.writeln (' typeof confirm: ' + typeof confirm + ' <br> '); Document.writeln (' typeof confirm: ' + typeof window.open + ' <br> '); Document.writeln (' typeof alert: ' + typeof alert + ' <br> '); Document.writeln (' typeof Window.alert: ' + typeof Window.alert + ' <br> '); Document.writeln (' typeof document.getElementById: ' + typeof document.getElementById + ' <br> '); Document.writeln (' typeof document.createelement: ' + typeof document.createelement + ' <br> '); </script> <p> test the original Isfunction function method </p> <script> var isfunction = function (FN, name) {return DOCUMENT.WR Iteln (' isfunction (' + name + '): ' + (Object.prototype.toString.call (fn) = = "[Object Function]") + ' <br> '); } isfunction (eval, ' eval '); Isfunction (Confirm, ' confirm '); Isfunction (window.open, ' window.open '); Isfunction (Alert, ' Alert '); Isfunction (Window.alert, ' Window.alert '); Isfunction (document.getElementById, ' document.getElementById '); Isfunction (document.createelement, ' document.createelement '); Isfunction (isfunction, ' isfunction '); </script> <p> Test Repair isfunction function method </p> <script> var isfunction = (function () {return type of document.getElementById = = "Object"? Isfunction = function (FN, name) {try {return Document.writeln, ' isfunction (' + name + '): ' + (/^\s*\bfunction\b/.test (" "+ fn)" + ' <br> '); catch (x) {return Document.writeln (' isfunction (' + name + '): ' + (false) + ' <br> '); }}: ISfunction = function (FN, name) {return Document.writeln (' isfunction (' + name + '): ' + (' [object function] ' = = OBJECT.PR Ototype.toString.call (FN)) + ' <br> '); }; }) () Isfunction (eval, ' eval '); Isfunction (Confirm, ' confirm '); Isfunction (window.open, ' window.open '); Isfunction (Alert, ' Alert '); Isfunction (Window.alert, ' Window.alert '); Isfunction (document.getElementById, ' document.getElementById '); Isfunction (document.createelement, ' document.createelement '); Isfunction (isfunction, ' isfunction '); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

It will be found that using TypeOf to detect alert, confirm method, and DOM methods under IE shows object, while other browsers display function.

So how to perfect this problem?

    1. typeof to detect whether a method (for example: document.getElementById) is object and how it is, rewrite the isfunction function;
    2. How do you rewrite it? A regular judge of the incoming object string ("" + fn), whether the starting position contains a function, namely:/^\s*\bfunction\b/.test ("+ FN").

OK, look at the modified Isfunction function based on the above ideas:

Copy Code code as follows:

var isfunction = (function () {//performance Optimization:lazy function Definition return "object" = = typeof document.g Etelementbyid? Isfunction = function (fn) {try {return/^\s*\bfunction\b/.test ("" + fn);} catch (x) {return false}}: Isfunction = function (fn) {return "[object function]" = = Object.prototype.toString.call (FN) ; };}) ()

Reference reading:

    • "Isfunction hacked, iscallable solution"
    • isfunction () or IsObject (), which is the question?
    • "Lazy Function Definition pattern"

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.