Prev: A practical exercise: fighting maskons
Next: FAQ Topic - How do I get a jsp/php variable into client-side javascript? (2009-11-10)
From: kangax on 29 Nov 2009 23:45 Garrett Smith wrote: > kangax wrote: >> Garrett Smith wrote: >>> Richard Cornford wrote: >>>> kangax wrote: >>>>> Garrett Smith wrote: >>>> <snip> >>> [...] >>> >>>> A (one of the many) bugbears of - isFunction - Firefox's response to:- >>>> >>>> var x = document.createElement('OBJECT'); >>>> >>>> - which, when tested with - typeof - returns 'function'. This is >>>> seen as incorrect, e.g.:- >>>> >>>> <URL: http://jsninja.com/Functions#Function_Type > >>>> >>> >>> That code was reviewed here over a year ago and was changed in jQuery: >>> >>> | isFunction: function( obj ) { >>> | return toString.call(obj) === "[object Function]"; >>> | } >>> >>> What surprises me is that wiki document was modified very recently: >> >> Resig says that example now uses [[Class]] check. >> > > Then he is mistaken. The site has the same code: How is he mistaken? The section *now uses* [[Class]] check (as Resig said). I guess that wiki is just not the latest version of the book. > http://jsninja.com/Functions#Function_Type > > // Do not use this function: > | function isFunction( fn ) { > | return !!fn && !fn.nodeName && fn.constructor != String && > | fn.constructor != RegExp && fn.constructor != Array && > | /function/i.test( fn + "" ); > | } > > Nobody should be using that code at this time. That isFunction relies on > function decompilation. It fails across frames. It provides wrong > results for some cases, the most obvious being any object whose toString > contains substring "function" (any case). > > var book = { > title : "functional javascript", > valueOf : function(){ return this.title; } > }; > > That isFunction will result true. > > The justification for that function is based on the method overloading > strategies used in the initial design of jQuery. > > We've been over and over this. I blogged about it with many examples, in > 2007. That lousy function has gotten a lot of criticism. It seems hard Yes I remember. Your blog is where I originally found out about function decompilation (and its fragile nature) :) > to believe that Resig could be unaware of that criticism. > > So why is he writing about it advocating its use? Making a one-time > mistake is understandable. Advocating the use a well-known mistake > defies explanation. It's just an old text. I don't think he's advocating anything like this any more. -- kangax |