Skip to content

Commit 6f70518

Browse files
committed
Merge pull request #83 from doctorme/master
Simplified and sped up the "type-function" example.
2 parents 46a0b9e + a97b9d1 commit 6f70518

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Diff for: chapters/classes_and_objects/type-function.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ You'd like to know the type of a object without using typeof. (See http://javasc
1212
Use 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

0 commit comments

Comments
 (0)