Re: When is a function not a function?



Matt Kruse wrote:
On Nov 26, 1:54 pm, Richard Cornford wrote:
You might reasonably say that if - typeof x - was 'function'
then it would be safe to call the object, and be correct in
all the environments that I have ever encountered.

Unfortunately, in Firefox:

var o = document.createElement("object");
alert(typeof o); // "function"
o(); // ERROR

You are making a surprisingly obvious mistake here. You are assuming that because calling a function results in an exception being thrown then that means that the function/object is not callable. When you think about that it is pretty obvious that such a conclusion is not valid.

In Firefox, when you call an object that is not callable the exception thrown is "o is not a function", but the exception here is "Component is not available", so you cannot prove that the object is not callable, only that calling it throws an exception. It could be that if and when the "Component" (whatever that may be in this case) was available you could happily call the function/object without an exception being thrown.

The bottom line is that an object's being callable is no guarantee that calling will be an error free operation.

<snip>
This is actually the origin of the above case that
fails in FF.

So this is not your mistake, you just didn't think about it enough to see who was being fooled here.

Richard.

.



Relevant Pages

  • Re: Assertion on Type conversion
    ... The author states that "if a method has specified some pre-condition then the failure of that condition is the responsibility of the ... depends on the caller's ability to check for conditions, and assure them to be correct, prior to the calling of the method. ... An overlooked exception that could be handled gracefully. ... Implement a method like "IsValid" to do a runtime check first if your concerned about the ArgumentException being thrown. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Microsoft.XLANGs.RuntimeTypes.InvalidPropertyTypeException
    ... Trying to access a data property on a message for which it has not ... Yes the exception is of significant concern; it is a major user error. ... Any exception thrown could delay the cleanup of resources, ... System.Text.StringBuilder.Append), calling mscorsvr!Ordinal76+0x1d925 ...
    (microsoft.public.biztalk.server)
  • Re: When is a function not a function?
    ... the exception thrown is "o is not a function", ... from throwing an exception) or by testing that conditions that would result in an exception being thrown don't apply before taking actions that may result in their being thrown. ... The act of calling the object has not been shown to throw an error. ... Given that knowing something is callable is never enough to tell you that calling it will be error free you are just describing a stupid design and then saying that the aspects of the design that make it stupid become a problem in context. ...
    (comp.lang.javascript)
  • Catching exceptions but dont want to cast the type.
    ... thrown (I'm calling a web service in the try) and what I would ideally like ... need to have sort of a generic exception where I can then say - what type is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Exceptions, Error Codes, and DLL development
    ... This means that only exception which are "thrown" will be caught. ... example raises an os-exception. ... So this is your mistake:) ...
    (borland.public.delphi.non-technical)