File tree Expand file tree Collapse file tree 1 file changed +14
-10
lines changed
chapters/classes_and_objects Expand file tree Collapse file tree 1 file changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -12,16 +12,20 @@ You'd like to know the type of a object without using typeof. (See http://javasc
1212Use the following function:
1313
1414{% highlight coffeescript %}
15- type = (obj) ->
16- if obj == undefined or obj == null
17- return String obj
18- classToType = new Object
19- for name in "Boolean Number String Function Array Date RegExp".split(" ")
20- classToType[ "[ object " + name + "] "] = name.toLowerCase()
21- myClass = Object.prototype.toString.call obj
22- if myClass of classToType
23- return classToType[ myClass]
24- return "object"
15+ type = (obj) ->
16+ if obj == undefined or obj == null
17+ return String obj
18+ classToType = {
19+ '[ object Boolean] ': 'boolean',
20+ '[ object Number] ': 'number',
21+ '[ object String] ': 'string',
22+ '[ object Function] ': 'function',
23+ '[ object Array] ': 'array',
24+ '[ object Date] ': 'date',
25+ '[ object RegExp] ': 'regexp',
26+ '[ object Object] ': 'object'
27+ }
28+ return classToType[ Object.prototype.toString.call(obj)]
2529{% endhighlight %}
2630
2731## Discussion
You can’t perform that action at this time.
0 commit comments