File tree 1 file changed +14
-10
lines changed
chapters/classes_and_objects
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
12
12
Use the following function:
13
13
14
14
{% 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)]
25
29
{% endhighlight %}
26
30
27
31
## Discussion
You can’t perform that action at this time.
0 commit comments