色んな所で罵倒されてるjavascript/coffeescriptの型判定関数ですが。
パパッと使い勝手のいい関数で補完しましょう。
方法一、JavaScript Garden参照
typeOf = (obj) ->
Object.prototype.toString.call(obj)[8...-1]
方法二、CoffeeScript Cookbook参照
要注意!
ここでのエラーはオブジェクトと判定されます(ある意味正しい)
typeOf(new Error()) === 'object'
typeOf = (obj) ->
if obj == undefined or obj == null
return String obj
classToType = new Object
for name in "Boolean Number String Function Array Date RegExp".split(" ")
classToType["[object " + name + "]"] = name.toLowerCase()
myClass = Object.prototype.toString.call obj
if myClass of classToType
return classToType[myClass]
return "object"