473,806 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

isFunction (Code Worth Recommending Project)

I see the "When is a function not a function" thread has flared up
again.

This is what I use to test parameters that can be Functions or
Objects. It does return the expected result for callable host objects
(at least the ones I tested.) None of the functions it supports are
expected to receive those as parameters. The second part of the test
is a little ambiguous in this regard (for browsers that do not support
call), so it is best to exclude callable host objects as a rule.

var isFunction = function(o) {
return typeof(o) == 'function' && (!Function.prot otype.call ||
typeof(o.call) == 'function');
};
Dec 8 '07 #1
13 8083
David Mark wrote:
I see the "When is a function not a function" thread has flared up
again.

This is what I use to test parameters that can be Functions or
Objects. It does return the expected result for callable host objects
(at least the ones I tested.) None of the functions it supports are
expected to receive those as parameters. The second part of the test
is a little ambiguous in this regard (for browsers that do not support
call), so it is best to exclude callable host objects as a rule.

var isFunction = function(o) {
return typeof(o) == 'function' && (!Function.prot otype.call ||
typeof(o.call) == 'function');
};
isFunction() will return `false' where Function.protot ype.call is not
supported and the object has no `call' property, even though the argument
referred to a Function object. That is the case in JavaScript before
version 1.3 as supported by Netscape Navigator, version 4.05 and earlier,
and, more important, JScript before version 5.5 as supported e.g. by
Microsoft Internet Explorer for Windows NT, version 5.01 and earlier.

The argument may be an unqualified reference in which case calling this
testing method fails if the identifier of that reference was not defined before.

There is no point in assigning a function object created by a function
expression in the initialization of a variable instead of a simple function
declaration, unless that code is part of a conditional execution block.

`typeof' is an operator, not a method, and should be written accordingly.
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>
Dec 8 '07 #2
On Dec 8, 11:05 am, David Mark <dmark.cins...@ gmail.comwrote:
On Dec 8, 12:22 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
David Mark wrote:
On Dec 8, 11:07 am, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
>David Mark wrote:
>>On Dec 8, 9:33 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
>>wrote:
>>>David Mark wrote:
>>>>I see the "When is a function not a function" thread has flared
>>>>up again. This is what I use to test parameters that can be
>>>>Functions or Objects. It does return the expected result for
>>>>callable host objects (at least the ones I tested.) None of the
>>>>functions it supports are expected to receive those as
>>>>parameter s. The second part of the test is a little ambiguous in
>>>>this regard (for browsers that do not support call), so it is
>>>>best to exclude callable host objects as a rule. var isFunction =
>>>>function( o) { return typeof(o) == 'function' &&
>>>>(!Function. prototype.call || typeof(o.call) == 'function'); };
>>>isFunction () will return `false' where Function.protot ype.call is
>>>not supported and the object has no `call' property, even though
>>>the argument referred to a Function object. That is the case in
>>>JavaScript before
>>If it is a Function then the first part of the conjunction is true.
>>The second part will also be true as !Function.proto type.call will
>>evaluate to true when call is not supported. So just what are you
>>talking about?
>Sorry, I have confused matters here. However, I would like to point
>out that it does not make sense to test against
>Function.proto type.call, be it in the negative direct or the positive
>indirect way. That the object has a(n *external*) `call' property has
>nothing to do with whether or not it is callable (with an argument
>list), having an *internal* [[Call]] method.
It is not meant to return true for anything but Functions. It is not an
isCallable function.
Again, if I already knew that the argument is a reference to a Function
object, I would not need (to call) your method. Because Function objects

You apparently haven't been paying attention. Let me try to simplify
this for you. This function for my purposes could be as simple as:

typeof(o) == 'function'

The calling functions do NOT know whether their argument(s) are
Functions or some other type of object. When they ARE DETERMINED TO
BE FUNCTIONS by this test, they are called. When not, a method of the
object is called. It was created specifically for callbacks as I
already mentioned. How you translate that into "I already knew the
argument is a reference to a Function object" after this has been
explained three times already is beyond me.

If you go back and read the original thread, you will understand why
the additional logic was added (to weed out callable host objects and
methods.)
I know a callable host object may not be a "function" but if I was
using a function called "isFunction " I'd expect that if I send it any
callable object it would return true.
It is more of an academic exercise than anything and after
thinking about it, I don't think it needs to be in the repository.
I agree (at least for now). I think the repository should gain
foundational functions like this as needed for bigger pieces of code
(e.g. Ajax, events, etc). I've never created an overloaded JavaScript
function where the overloaded argument could be a function or not. As
far as I know, I've only ever had an overloaded argument for an
element or an element id string. Because of this I haven't really
followed along with the various "when is a function not a function"
threads.

[snip]

--
Peter
Code Worth Recommending Project
http://cljs.michaux.ca
Dec 9 '07 #3
On Dec 8, 10:32 pm, RobG <rg...@iinet.ne t.auwrote:
Peter Michaux wrote:
On Dec 8, 11:05 am, David Mark <dmark.cins...@ gmail.comwrote:
>
It is more of an academic exercise than anything and after
thinking about it, I don't think it needs to be in the
repository.
I agree (at least for now). I think the repository should gain
foundational functions like this as needed for bigger pieces of
code (e.g. Ajax, events, etc).

There seems to be agreement that determining whether an object is a
function is not a reliable indicator of whether it may be called, and
that there is a general need to determine whether or not something is
callable. Therefore, there is a need for an isCallable function.
It seems like such a function is at least difficult to write and
people have been getting by without it. It is certainly possible to
write all the basic parts of a browser scripting library without such
a function. If there is a general interest in having such a function
in the repository and such a function can be written to perform
reliably then it should be added to the repository.

--
Peter
Code Worth Recommending Project
http://cljs.michaux.ca
Dec 9 '07 #4
Peter Michaux wrote:
On Dec 8, 10:32 pm, RobG <rg...@iinet.ne t.auwrote:
>Peter Michaux wrote:
>>On Dec 8, 11:05 am, David Mark <dmark.cins...@ gmail.comwrote:
It is more of an academic exercise than anything and after
thinking about it, I don't think it needs to be in the
repository .
I agree (at least for now). I think the repository should gain
foundationa l functions like this as needed for bigger pieces of
code (e.g. Ajax, events, etc).
There seems to be agreement that determining whether an object is a
function is not a reliable indicator of whether it may be called, and
that there is a general need to determine whether or not something is
callable. Therefore, there is a need for an isCallable function.

It seems like such a function is at least difficult to write and
people have been getting by without it. It is certainly possible to
write all the basic parts of a browser scripting library without such
a function. If there is a general interest in having such a function
in the repository and such a function can be written to perform
reliably then it should be added to the repository.
Yes, and at the end of this exploration we are back to:
1. For host methods (aka feature detection), use:

if (hostObj && hostObj.method) {
hostObj.method( );
}
2. If you know fn isn't a host method but should be
a native function, use:

if (typeof fn == 'function') {
fn();
}

3. If you don't know (and these cases should be kept
to a minimum), use:

if (typeof obj == 'function' || typeof obj == 'object') {
obj();
}
where the term "function" used above means a native object that
implements [[call]].
P.S. I agree with Thomas about not using brackets with typeof. :-)
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Dec 9 '07 #5
On Dec 9, 1:52 am, Peter Michaux <petermich...@g mail.comwrote:
On Dec 8, 10:32 pm, RobG <rg...@iinet.ne t.auwrote:
Peter Michaux wrote:
On Dec 8, 11:05 am, David Mark <dmark.cins...@ gmail.comwrote:
It is more of an academic exercise than anything and after
thinking about it, I don't think it needs to be in the
repository.
I agree (at least for now). I think the repository should gain
foundational functions like this as needed for bigger pieces of
code (e.g. Ajax, events, etc).
There seems to be agreement that determining whether an object is a
function is not a reliable indicator of whether it may be called, and
that there is a general need to determine whether or not something is
callable. Therefore, there is a need for an isCallable function.

It seems like such a function is at least difficult to write and
people have been getting by without it. It is certainly possible to
write all the basic parts of a browser scripting library without such
a function. If there is a general interest in having such a function
in the repository and such a function can be written to perform
reliably then it should be added to the repository.
The primary need for this is to feature test host methods. It is
covered by isFeaturedMetho d in the gEBI wrapper thread.

Dec 9 '07 #6
On Dec 9, 2:20 am, RobG <rg...@iinet.ne t.auwrote:
Peter Michaux wrote:
On Dec 8, 10:32 pm, RobG <rg...@iinet.ne t.auwrote:
Peter Michaux wrote:
On Dec 8, 11:05 am, David Mark <dmark.cins...@ gmail.comwrote:
It is more of an academic exercise than anything and after
thinking about it, I don't think it needs to be in the
repository.
I agree (at least for now). I think the repository should gain
foundational functions like this as needed for bigger pieces of
code (e.g. Ajax, events, etc).
There seems to be agreement that determining whether an object is a
function is not a reliable indicator of whether it may be called, and
that there is a general need to determine whether or not something is
callable. Therefore, there is a need for an isCallable function.
It seems like such a function is at least difficult to write and
people have been getting by without it. It is certainly possible to
write all the basic parts of a browser scripting library without such
a function. If there is a general interest in having such a function
in the repository and such a function can be written to perform
reliably then it should be added to the repository.

Yes, and at the end of this exploration we are back to:

1. For host methods (aka feature detection), use:

if (hostObj && hostObj.method) {
hostObj.method( );
}
I consider that insufficient. For one, it will error on methods of
ActiveX objects.
>
2. If you know fn isn't a host method but should be
a native function, use:

if (typeof fn == 'function') {
fn();
}

3. If you don't know (and these cases should be kept
to a minimum), use:

if (typeof obj == 'function' || typeof obj == 'object') {
obj();
}
This is a partial solution to feature testing host methods. This is
what I use:

var reFeaturedMetho d = new RegExp('^functi on|object$', 'i');

var isFeaturedMetho d = function(o, m) {
var t = typeof(o[m]);
return !!((reFeaturedM ethod.test(t) && o[m]) || t == 'unknown');
};

That covers ActiveX methods and null objects. As Thomas pointed out
(repeatedly), o must be a full qualified reference, which seems to be
an issue for him, though I don't consider it a problem.
Dec 9 '07 #7
RobG wrote:
1. For host methods (aka feature detection), use:

if (hostObj && hostObj.method) {
hostObj.method( );
}
I don't think so, and I have already explained why.
2. If you know fn isn't a host method but should be
a native function, use:

if (typeof fn == 'function') {
fn();
}

3. If you don't know (and these cases should be kept
to a minimum), use:

if (typeof obj == 'function' || typeof obj == 'object') {
obj();
}
Generally, ACK to those two. However, the very problem here is that
you _never_ know. As I have said, we would not need feature detection
if we already knew :)
where the term "function" used above means a native object that
implements [[call]].
1. [[Call]]

2. AIUI, it would not necessarily be *an implementation of* [[Call]],
as
host objects may perform calling in any way the implementor saw
fit.
P.S. I agree with Thomas about not using brackets with typeof. :-)
Thanks. However, I did not suggest to avoid parentheses in a `typeof'
operation altogether. There is nothing wrong with writing those; in
fact, it may be necessary to force precedence. I suggested instead
that the `typeof' keyword be followed by space so as to indicate it is
an operator and not, as its being followed directly by a parenthesed
expression would indicate, a method. (Of course, if parentheses are
not used, that would require `typeof' to be followed by whitespace,
else it would not be parsed as a keyword.)
Regards,

PointedEars
Dec 10 '07 #8
On Dec 10, 8:38 am, "Thomas 'PointedEars' Lahn" <PointedE...@we b.de>
wrote:
[snip]
>
Thanks. However, I did not suggest to avoid parentheses in a `typeof'
operation altogether. There is nothing wrong with writing those; in
fact, it may be necessary to force precedence. I suggested instead
that the `typeof' keyword be followed by space so as to indicate it is
an operator and not, as its being followed directly by a parenthesed
expression would indicate, a method. (Of course, if parentheses are
not used, that would require `typeof' to be followed by whitespace,
else it would not be parsed as a keyword.)
Yes, I agree with that.
Dec 10 '07 #9
On Dec 8, 11:30 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
David Mark wrote:
On Dec 8, 12:22 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
David Mark wrote:
On Dec 8, 11:07 am, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
David Mark wrote:
On Dec 8, 9:33 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
David Mark wrote:
>I see the "When is a function not a function" thread has flared
>up again. This is what I use to test parameters that can be
>Function s or Objects. It does return the expected result for
>callable host objects (at least the ones I tested.) None of the
>function s it supports are expected to receive those as
>parameters . The second part of the test is a little ambiguous in
>this regard (for browsers that do not support call), so it is
>best to exclude callable host objects as a rule. var isFunction =
>function(o ) { return typeof(o) == 'function' &&
>(!Function .prototype.call || typeof(o.call) == 'function'); };
isFunction( ) will return `false' where Function.protot ype.call is
not supported and the object has no `call' property, even though
the argument referred to a Function object. That is the case in
JavaScrip t before
If it is a Function then the first part of the conjunction is true.
The second part will also be true as !Function.proto type.call will
evaluate to true when call is not supported. So just what are you
talking about?
Sorry, I have confused matters here. However, I would like to point
out that it does not make sense to test against
Function.prot otype.call, be it in the negative direct or the positive
indirect way. That the object has a(n *external*) `call' property has
nothing to do with whether or not it is callable (with an argument
list), having an *internal* [[Call]] method.
It is not meant to return true for anything but Functions. It is not an
isCallable function.
Again, if I already knew that the argument is a reference to a Function
object, I would not need (to call) your method. Because Function objects
You apparently haven't been paying attention.

It is certain now that you don't know what you are doing.
Let me try to simplify this for you. This function for my purposes
could be as simple as:
typeof(o) == 'function'
David, then why isn't the function that simple?

Exactly my point. You don't need a function to test that at all.
"PointedEar s", would you recommend simply writing typeof(o) ==
'function' inline where David would write the function call?

A function that will also have the drawback, as compared to the already
available alternative, of requiring the caller to provide a qualified reference.
I can see this point. I think David is suggesting he always knows that
the 'o' variable is at least declared and so the call to the wrapper
function will not error.
[snip]

--
Peter
Code Worth Recommending Project
http://cljs.michaux.ca
Dec 12 '07 #10

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

Similar topics

1
2105
by: HyperX | last post by:
Hey guys, How can I see the code of the installer class? I know that I have to right click on my object class, -> Add Item -> Code -> installer class... The actual question is, I cannot see "Code" in my "Add Item" Menu. In order to see this 'Add Installer code'... Do I need to install any plugin?
7
1197
by: melinda | last post by:
What are some possible reasons for this: I call a static method from a class within my assembly...works fine. I call the same static method after it has been compiled into a different assembly and referenced to my project....does't work. Give's me a "Object not set to an instance...". What are some possible reasons for this? I'm just giving the assembly a strong name, compiling it into a dll and adding it as a reference into my...
17
3854
by: Sven Rutten | last post by:
Hello Actually I want to add some C#-Code to a VB.NET-Project in VS 2005. Normally I creating a DLL and importing that from the VB-Project. But as I am coding something for a Smart Device (PocketPC), I dont want to distribute multiple files, I'd like to distribute a single EXE file. I've learned that I can, since VS 2005, add C#-Code to a VB Project. I can add it, but not use it. What am I doing wrong?
2
1841
by: kanepart2 | last post by:
Hey all , I wanted to create a project or item template in C# such that when I use that template a parameter wizard comes up asking for values. I know how to create the template without implementing the wizard, i.e., work the parameter replacement using the <CustomParameterskeyword in XML. So any hints or suggestions towards the procedure to having the parameter replacement wizard (a GUI of some sort) would be greatly appreciated,
13
4104
by: Peter Michaux | last post by:
encodeURIComponent is commonly used to serialize forms for use with XMLHttpRequest requests. For a perfect simulation of browser form requests, the goal is to serialize form data in the <URL: http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.13.4.1> application/x-www-form-urlencoded</astandardized format. The handling of whitespace by encodeURIComponent() is different from the x-www-form-urlencoded standard....
0
9719
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
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10369
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
10110
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...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
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
6877
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
5546
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
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.