diff --git a/chapters/classes_and_objects/type-function.md b/chapters/classes_and_objects/type-function.md index 42952ac..96e8cea 100644 --- a/chapters/classes_and_objects/type-function.md +++ b/chapters/classes_and_objects/type-function.md @@ -12,16 +12,20 @@ You'd like to know the type of a object without using typeof. (See http://javasc Use the following function: {% highlight coffeescript %} -type = (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" + type = (obj) -> + if obj == undefined or obj == null + return String obj + classToType = { + '[object Boolean]': 'boolean', + '[object Number]': 'number', + '[object String]': 'string', + '[object Function]': 'function', + '[object Array]': 'array', + '[object Date]': 'date', + '[object RegExp]': 'regexp', + '[object Object]': 'object' + } + return classToType[Object.prototype.toString.call(obj)] {% endhighlight %} ## Discussion