Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: typeof operator on a .NET method returns "object" - is that correct?

$
0
0
Hi,

Using ClearScriptConsole, I find:
-> host.Where
[HostMethod:Where]
-> typeof host.Where
object
That is, a method on a host object is not reported to be a "function", even though it can be called with (). Contrast this with the situation in IE or Chrome's debug consoles:
typeof console.log
"function"
Why is this important? Generic JavaScript utility functions often need the ability to sniff for "callable" values, which they do using typeof === "function".

I encountered this when trying to use JSON.stringify on a tree of objects, e.g. a JS array of host objects. I need to control how the host object is serialised, so (in accordance with how JSON.stringify works) I implemented toJSON as a method on my host class. But JSON.stringify uses typeof to check whether toJSON is a function. It gets the answer "object", so it does not call it.

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles





Latest Images