Permalink
Browse files

Add note about extending native objects, fixes #1

  • Loading branch information...
1 parent 621b59b commit c97b7b6e5b446c92917ba36c23f3a09caaa5c8b8 @fhemberger fhemberger committed with Mar 19, 2012
@@ -18,12 +18,13 @@ array.filter (x) -> x > 5
# => [6,7,8,9,10]
{% endhighlight %}
-In pre-EC5 implementations, extend the Array prototype to add a filter function which will take a callback and perform a comprension over itself, collecting all elements for which the callback is true.
+In pre-EC5 implementations, extend the Array prototype to add a filter function which will take a callback and perform a comprension over itself, collecting all elements for which the callback is true. Be sure to check if the function is already implemented before overwriting it:
{% highlight coffeescript %}
# Extending Array's prototype
-Array::filter = (callback) ->
- element for element in this when callback(element)
+unless Array::filter
+ Array::filter = (callback) ->
+ element for element in this when callback(element)
array = [1..10]
@@ -22,3 +22,7 @@ Array::unique = ->
## Discussion
There are many implementations of the `unique` method in JavaScript. This one is based on "The fastest method to find unique items in array" found [here](http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/).
+
+**Note:** Although it's quite common in languages like Ruby, extending native objects is often considered bad practice in JavaScript (see: [Maintainable JavaScript: Don’t modify objects you don’t own](http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/); [Extending built-in native objects. Evil or not?](http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/)).
+
+
@@ -21,3 +21,5 @@ Array::shuffle = -> @sort -> 0.5 - Math.random()
## Discussion
For more background on how this shuffle logic works, see this [discussion at StackOverflow](http://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling).
+
+**Note:** Although it's quite common in languages like Ruby, extending native objects is often considered bad practice in JavaScript (see: [Maintainable JavaScript: Don’t modify objects you don’t own](http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/); [Extending built-in native objects. Evil or not?](http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/)).
@@ -22,3 +22,5 @@ String::capitalize = () ->
## Discussion
Objects in JavaScript (and thus, in CoffeeScript) have a prototype member that defines what member functions should be available on all objects based on that prototype. In CoffeeScript, you can access the prototype directly via the `::` operator.
+
+**Note:** Although it's quite common in languages like Ruby, extending native objects is often considered bad practice in JavaScript (see: [Maintainable JavaScript: Don’t modify objects you don’t own](http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/); [Extending built-in native objects. Evil or not?](http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/)).
@@ -43,3 +43,5 @@ String.prototype.downcase = function() {
};
"ONE TWO THREE".downcase();
{% endhighlight %}
+
+**Note:** Although it's quite common in languages like Ruby, extending native objects is often considered bad practice in JavaScript (see: [Maintainable JavaScript: Don’t modify objects you don’t own](http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/); [Extending built-in native objects. Evil or not?](http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/)).
@@ -43,3 +43,5 @@ String.prototype.upcase = function() {
};
"one two three".upcase();
{% endhighlight %}
+
+**Note:** Although it's quite common in languages like Ruby, extending native objects is often considered bad practice in JavaScript (see: [Maintainable JavaScript: Don’t modify objects you don’t own](http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/); [Extending built-in native objects. Evil or not?](http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/)).

0 comments on commit c97b7b6

Please sign in to comment.