473,772 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When is a function not a function?

I've got a collection of functions that accept a function or object
(paired with a method name) as a callback.

For the longest time I have relied on this test.

(typeof cb == 'function')

This should work as well, but I am not sure how well it degrades in
older browsers. I think there are issues with functions created in
another context (eg frame) as well.

(cb instanceof Function)

Then I came across this.

(!!fn && typeof fn != "string" && !fn.nodeName && fn.constructor !=
Array && /function/i.test( fn + "" ))

I'm sure it is wrong as it is from jQuery, but it makes me wonder if
there are bugs in older browsers that will mislead my test.

As an aside, I was looking at the jQuery source as some browser
sniffer I was arguing with on a blog cited it as part of an "everybody
is doing it" argument. I had glanced at it previously and dismissed
it based on its resemblance to Prototype. Upon closer inspection, it
makes Prototype look inspired. Suffice to say that anybody who
recommends it hasn't read the code. It's a horror show.

Sep 26 '07
32 3528
John G Harris said the following on 9/27/2007 3:37 PM:
On Wed, 26 Sep 2007 at 20:21:44, in comp.lang.javas cript, Randy Webb
wrote:
>Thomas 'PointedEars' Lahn said the following on 9/26/2007 5:40 PM:
>>Peter Michaux wrote:
On Sep 26, 1:02 pm, David Mark <dmark.cins...@ gmail.comwrote:
jQuery uses dollar sign variables, which I don't think are allowed
under the specification.
Dollar signs are allowed. See sec 7.6. The spec then says "The dollar
sign is intended for use only in mechanically generated code."
Which is exactly why jQuery should not.
But there is no reason why it *can not* other than people's pedantic
desires to whine about it. Had it been S that was used instead, people
would shut up about it and move on. Except that if it had been S
instead of $ then people would be whining about how the name wasn't an
indication of what it did.


But it's already happened :

Thou shalt not start a global name with "MM_".
Or __, ever heard of __doPostBack and the others? All of which are
naming clashes.
Because otherwise there will come a day when you get an obscure bug that
will drive you crazy.
That is true with any prefix though. Not just $ or MM or __
There's a similar rule in C++ about names starting with leading
underlines. The difference is that the rule has been biting people for
years, so competent programmers don't dismiss it as pedantic standards
mania.
Nice to know that about C++ but I don't see the relationship between C++
and/or Java (that Richard brought into it).

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 27 '07 #11
dh**********@gm ail.com wrote:
On Oct 3, 9:59 am, Randy Webb <HikksNotAtH... @aol.comwrote:
>Richard Cornford said the following on 9/30/2007 4:36 PM:
>>Randy Webb wrote:
>It doesn't say "limited" or "required". It simply says "This is what it
is *intended* for". I doubt very seriously that 99% of what is done with
scripting was it's "intended use".

Either way, it doesn't matter what ECMA had to say about it, people are
still going to use it until some UA comes out that breaks it. And no
amount of discussing it in comp.lang.javas cript will ever change that.

Things that might seem not to matter:
{OOA&D, Testing, Variable names, comments, code formatting
conventions, process}

These things are just not that appealing to people, generally. This is
especially true for presentation layer development. Why not look at
what toolkit vendors do? They're the rock stars, right?

Yahoo still employs code freezes, status reports, and last time I was
there, my manager would not let me check ANY tests into CVS (they
still don't use SVN). They were calling this Scrum.

Little things like "don't use post-increment operator" are easy for
people to grasp. Free advice that comes with cookies and refreshments.
Serotonin levels go up with the increased blood sugar, jokes are told,
examples are shown (with much hand waving), and people are happy while
not working.

I do wonder though: Why $? I would like to know, out of curiosity, why
did jQuery use $? There is a reason the variable was used; I just
don't know what it is.

I would say ha about 90% of "standards" s not so much 'best practice' as
just plain simple 'do it this ways so that people coming upon you
unexpectedly will know what's going on'

E.g. we all drive on one side of the road, but which side is open to debate.

Standards are there mostly for people working together, to enforce a
common style, so everybody knows how things should be done.

Its a mark of the petty jobsworth to elect such standards into moral Laws.
>--
Randy
Chance Favors The Prepared Mind
comp.lang.java script FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptT oolbox.com/bestpractices/

Oct 5 '07 #12
dh**********@gm ail.com wrote:
I do wonder though: Why $? I would like to know, out of curiosity, why
did jQuery use $? There is a reason the variable was used; I just
don't know what it is.
Cluelessness. (That was easy.)
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Oct 5 '07 #13
Thomas 'PointedEars' Lahn said the following on 10/5/2007 9:14 AM:
dh**********@gm ail.com wrote:
>I do wonder though: Why $? I would like to know, out of curiosity, why
did jQuery use $? There is a reason the variable was used; I just
don't know what it is.

Cluelessness. (That was easy.)
And it is debatable who is the one possessing the "Cluelessne ss".

Why it was used is trivial to figure out.

P.S. YSCIB

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 5 '07 #14
Randy Webb wrote:
Thomas 'PointedEars' Lahn said the following on 10/5/2007 9:14 AM:
>dh**********@gm ail.com wrote:
>>I do wonder though: Why $? I would like to know, out of curiosity, why
did jQuery use $? There is a reason the variable was used; I just
don't know what it is.
Cluelessness . (That was easy.)

And it is debatable who is the one possessing the "Cluelessne ss".
Not understood.
Why it was used is trivial to figure out.
Then you can tell it for sure, yes?
P.S. YSCIB
-v
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Oct 5 '07 #15
Thomas 'PointedEars' Lahn said the following on 10/5/2007 7:37 PM:
Randy Webb wrote:
>Thomas 'PointedEars' Lahn said the following on 10/5/2007 9:14 AM:
>>dh**********@gm ail.com wrote:
I do wonder though: Why $? I would like to know, out of curiosity, why
did jQuery use $? There is a reason the variable was used; I just
don't know what it is.
Cluelessnes s. (That was easy.)
And it is debatable who is the one possessing the "Cluelessne ss".

Not understood.
Try enabling the spell checker in Thunderbird. It might shed some light
on it.
>Why it was used is trivial to figure out.

Then you can tell it for sure, yes?
Sure I can. It was chosen because of the uniqueness of it and the lack
of use of it on the web. What better way to pick a simple single
character identifier than one that wasn't in use?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 6 '07 #16
Randy Webb wrote:
Thomas 'PointedEars' Lahn said the following on 10/5/2007 7:37 PM:
>Randy Webb wrote:
>>Thomas 'PointedEars' Lahn said the following on 10/5/2007 9:14 AM:
dh**********@gm ail.com wrote:
I do wonder though: Why $? I would like to know, out of curiosity, why
did jQuery use $? There is a reason the variable was used; I just
don't know what it is.
Cluelessness . (That was easy.)
And it is debatable who is the one possessing the "Cluelessne ss".
Not understood.

Try enabling the spell checker in Thunderbird. It might shed some light
on it.
It merely shows that you don't know the word and that it isn't in the
default dictionary of Thunderbird's spell checker.
>>Why it was used is trivial to figure out.
Then you can tell it for sure, yes?

Sure I can.
You can *not*, you are not the author.
It was chosen because of the uniqueness of it and the lack
of use of it on the web.
Logical fallacy: wishful thinking. You can know neither whether or not it
was used on the Web at the time the library was conceived, nor can you know
that supposed uniqueness or supposed no-use at the time was the reason for
choosing it.
What better way to pick a simple single character identifier than one
that wasn't in use?
Logical fallacy: non sequitur.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Oct 6 '07 #17
Thomas 'PointedEars' Lahn said the following on 10/6/2007 7:53 AM:
Randy Webb wrote:
>Thomas 'PointedEars' Lahn said the following on 10/5/2007 7:37 PM:
>>Randy Webb wrote:
Thomas 'PointedEars' Lahn said the following on 10/5/2007 9:14 AM:
dh**********@gm ail.com wrote:
>I do wonder though: Why $? I would like to know, out of curiosity, why
>did jQuery use $? There is a reason the variable was used; I just
>don't know what it is.
Cluelessnes s. (That was easy.)
And it is debatable who is the one possessing the "Cluelessne ss".
Not understood.
Try enabling the spell checker in Thunderbird. It might shed some light
on it.

It merely shows that you don't know the word and that it isn't in the
default dictionary of Thunderbird's spell checker.
<eyerollYou still don't get it. I didn't expect you to though.
>>>Why it was used is trivial to figure out.
Then you can tell it for sure, yes?
Sure I can.

You can *not*, you are not the author.
You need to get out into the real world more often Thomas.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 6 '07 #18
dh**********@gm ail.com wrote:
On Oct 13, 2:41 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
>The code comes from jQuery (1.2.1):

// This may seem like some crazy code, but trust me when I say that this
// is the only cross-browser way to do this. --John
isFunction:func tion( fn ) {
return !!fn && typeof fn != "string" && !fn.nodeName &&
fn.constructor != Array && /function/i.test( fn + "" );
}

jQuery claims to support IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+ (and
calls that "cross-browser" -- OMG). But, if

/function/i.test(fn + "")

would return `true', it is highly unlikely that

typeof fn

would yield "string", so the test

typeof fn != "string"

is unnecessary. Unless, of course, someone who lacks the minimum clue
assumes that strings could be called and so could ever be subject to
isFunction() .

No, it excludes strings.
Which is unnecessary, as strings could never be called, and so should never
be subject to the isFunction() test method.
"function" + "" would return true. This clause prevents that.
But can you name a case where a callable object would evaluate as a string
containing "function" without type conversion?
>Furthermore, if

/function/i.test(fn + "")

would return `true', how could it be that

fn.constructor != Array

var a = Array{function bork(){});
But that assumes that `a' could be called which it cannot be. Hence it
should not be subject to an isFunction() test. An array is not a function.
>couldnotyiel d `true'
Again, I did not write it that way. Please stop using broken software.
and therefore that test would be unnecessary as well?
No.
Yes.
>Needless to say that RegExp.prototyp e.test() already type-converts its
argument to string (see ECMAScript Ed. 3, 15.10.6.3) and so the forced
conversion by concatenation is unnecessary.

That's true, but being explicit is a good thing.
Being explicit would be

/.../.test(String(.. .))

But that would be nonsense as well, as it would decrease efficiency.
Because if the specification algorithm is followed, type conversion
to String would happen *twice* in both cases: first due to the source
code and the second time as step 1 of RegExp.prototyp e.exec().
>And aFunctionobject , being a native object, certainly could have a
user-defined property `nodeName' and still could be called. So that
test makes no sense as well.

Right, so could any of the four examples on my site.
I didn't care about your (Web?) site and I have no intention to do so. If
you have arguments to present, please do it here.
>- False-values could only yield `object', as `null' does, but they wouldnot
type-convert to `true';

- typeof fn == "string" is already excluded (unless one assumes the
improbable case that `typeof' would yield "stringfunction " and the like);

new String()
{toString:funct ion(){return 'function';}}
This is assuming that a String object could be called and so should/could
ever be subject to isFunction().
a = Function();
a.toString = "widget";
What should this accomplish? Overwriting the toString() method with a
string value renders type conversion to string non-functional. And even
if you meant

a = Function();
a.toString = function() { return "widget"; };

`typeof a' still would yield "function". The flaw in the described
algorithm is that it does not use `typeof' first. Because if it did,
many tests would not be necessary and the algorithm as a whole would
be less obscure and less error-prone.
>- fn.constructor != Array could only yield `true' because typeof "function"
would apply;

frames.
I don't see any argument here.
String values do not imply an object's type. Relying on toString is
not safe.
Exactly.
PointedEars
Oct 14 '07 #19
Randy Webb wrote:
Richard Cornford said the following on 10/14/2007 11:18 AM:
>Randy Webb wrote:
>>Richard Cornford said the following on 10/13/2007 10:28 PM:
<dh********* *@gmail.comwrot e:
typeof fn? nope, not in safari.
Are you certain? Is it really the case that objects that
return 'function' from a typeof operation cannot be
called? Or are you just not expecting them to be callable?
Have you actually tried calling the objects that safari
asserts are functions?
Can I ask you to first define what you mean by "called"
or "callable"?
No more than sticking an ArgumentsList after a reference to the object
(the 'call operator', with or without arguments). That is what I would
consider calling an object, and to be callable that abject should not
throw an exception as a direct result of its being called (though it may
still throw an exception in response to its arguments'' values, as that
would be behaviour passed the point of calling it and so unrelated to
its callability).

That would imply, to me anyway, that Safari (even in Beta form) gets it
right and all other browsers I tested it on get it wrong. Would that be
a correct assumption?
Not at all. With host objects, all bets are off. And as long as objects
that can't be called do not yield `function' or `object', there is no problem.
>>All other Windows based browsers I have report it as 'object'.
Maybe, but that is neither necessarily true of windows based browsers
nor historically common on Mac browsers. Even Mac IE 5 would report
'function' in this context, and on Windows you only have to go back a
few Opera versions to see the same there.

Then Safari is right and IE/FF/Opera are wrong?
It is not a matter of right and wrong.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Oct 14 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7827
by: lawrence | last post by:
Suppose I create dynamic web pages with 3 functions (which call other functions to make everything happen, but these 3 you might think of as being the top layer): registerSessions(); sendHtmlToBrowsers(); incrementPageViews(); Is there any chance that incrementPageViews() will be executed? Or, to
7
1391
by: Alan Holloway | last post by:
Hi all, Firstly thanks for your golden insight on my earlier post re bitwise operations. I now have another question! I've just been reading the excellent Peter van der Linden excerpt from sun.com (i'm ordering the book when I get paid), and I am getting to grips with the rules for understanding the complex declarations,
4
3626
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
4
11363
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error LNK2020: unresolved token (0A000007) memset LINK : error LNK2020: unresolved token (0A000008) free LINK : error LNK2020: unresolved token (0A00000A) atexit LINK : error LNK2020: unresolved token (0A000028) wcscpy LINK : error LNK2020: unresolved...
9
4923
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work fine from a link. The button does bring up the popup window, but when I press the links on the page, it doesn't return or close the window. ****************************************************************************
5
8631
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
1
1977
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following statement. Help.ShowHelp(Parent:=Me, url:=Me.HELP_URL_PRE & Me.myWorker.HelpFile) How do I get it to go away when the program exits? Now when I quit the program that I called it form the help file is sill displayed. Is there a way to get a handle...
1
2070
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has anyone else had this sort of problem with javascript? and any ideas how to fix it would be greatly appreciated.. I have included a copy of the code below, thanks. /**
6
1826
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool. One of the requirements is that the calendar will need to display a varying number of months (1..3)
1
3653
anfetienne
by: anfetienne | last post by:
i have this code below that i made....it loads vars from txt file splits it then puts it into an array....once in an array it the brings the pics in from the array to create thumbnails and a larger image. my problem is i have captions to go with it and when i try to load the captions nothing happens or can be seen to be happening. i dont know where i am going wrong as i have no output or compiled errors var locVar = new Array();...
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.