@@ -79,11 +79,11 @@ The following code adds the shuffle function to the Array prototype, which means
7979you are able to run it on any array you wish, in a much more direct manner.
8080
8181{% 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]
8484 j = Math.floor Math.random() * (i + 1)
8585 [ @[ i] , @[ j]] = [ @[ j] , @[ i]]
86- @
86+ this
8787
8888[ 1..9] .shuffle()
8989# => [ 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
9797operator (` ?= ` ). That way, we don't overwrite someone else's, or a native browser method.
9898
9999Also, 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
101101features, like maps and forEach, in a cross-browser, lean, high-performance way.
102102
103+ ^† [ Underscore] ( http://underscorejs.org/ ) is also a good alternative to Lo-dash.
104+
103105[ dontown ] : http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/
104106[ extendevil ] : http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/
0 commit comments