@@ -79,11 +79,11 @@ The following code adds the shuffle function to the Array prototype, which means
79
79
you are able to run it on any array you wish, in a much more direct manner.
80
80
81
81
{% highlight coffeescript %}
82
- do -> Array::shuffle ?= ->
83
- for i in [ @length-1 ..1]
82
+ Array::shuffle ?= ->
83
+ if @ length > 1 then for i in [ @length-1 ..1]
84
84
j = Math.floor Math.random() * (i + 1)
85
85
[ @[ i] , @[ j]] = [ @[ j] , @[ i]]
86
- @
86
+ this
87
87
88
88
[ 1..9] .shuffle()
89
89
# => [ 3, 1, 5, 6, 4, 8, 2, 9, 7 ]
@@ -97,8 +97,10 @@ objects you don’t own][dontown]; [Extending built-in native objects. Evil or n
97
97
operator (` ?= ` ). That way, we don't overwrite someone else's, or a native browser method.
98
98
99
99
Also, if you think you'll be using a lot of these utility functions, consider using a
100
- utility library, like [ Lo-dash] ( http://lodash.com/ ) . They include a lot of nifty
100
+ utility library, like [ Lo-dash] ( http://lodash.com/ ) ^† . They include a lot of nifty
101
101
features, like maps and forEach, in a cross-browser, lean, high-performance way.
102
102
103
+ ^† [ Underscore] ( http://underscorejs.org/ ) is also a good alternative to Lo-dash.
104
+
103
105
[ dontown ] : http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/
104
106
[ extendevil ] : http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/
0 commit comments