Add wrapper for typeof operator
#2
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a work in progress, and not yet ready to merge.I'm curious whether this is a good idea. I remember reading somewhere (I can't find it now) that Richard suggested "manual" reflection on
js.Objects to determine their type (i.e. checking for the existence of object methods as evidence for the object's type). The approach in this PR seems far easier to me when dealing with primitive types, and I suspect more efficient in many cases.My questions:
typeofto Go? Perhaps this has been discussed somewhere I haven't seen.typeofoperator in a function which isevaled). The eval runs only once, so the normal efficiency concerns of 'eval' shouldn't matter too much here. But perhaps there are other drawbacks I haven't considered.For background, the reason I care about this is that I have a jQuery Mobile event handler which passes either a DOM object or a URL string as one argument to a particular callback. At the moment, I'm determining the object type by comparing its
.String()output to[object Object], which works in my case (since a URL ought never equal that string), but direct access totypeofseems more fool-proof, and much simpler than writing complex heuristics in my code.If it is determined that this approach is basically sound, I still need to add additional error checking before merging. It's presently easy to crash the Typeof() function by passing certain JS-unfriendly data structures (e.g.
map[string]string{}or evenjs.Object{})