|
|
@@ -17,8 +17,8 @@ var arrayAugmentations = Object.create(Array.prototype)
|
|
|
.forEach(function (method) {
|
|
|
// cache original method
|
|
|
var original = Array.prototype[method]
|
|
|
- // define wrapped method
|
|
|
- _.define(arrayAugmentations, method, function () {
|
|
|
+
|
|
|
+ function mutator () {
|
|
|
// avoid leaking arguments:
|
|
|
// http://jsperf.com/closure-with-arguments
|
|
|
var l = arguments.length
|
|
|
@@ -79,7 +79,13 @@ var arrayAugmentations = Object.create(Array.prototype)
|
|
|
})
|
|
|
|
|
|
return result
|
|
|
- })
|
|
|
+ }
|
|
|
+ // define wrapped method
|
|
|
+ if (_.hasProto) {
|
|
|
+ _.define(arrayAugmentations, method, mutator)
|
|
|
+ } else {
|
|
|
+ arrayAugmentations[method] = mutator
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
@@ -91,12 +97,12 @@ var arrayAugmentations = Object.create(Array.prototype)
|
|
|
* @return {*} - replaced element
|
|
|
*/
|
|
|
|
|
|
-_.define(arrayAugmentations, '$set', function (index, val) {
|
|
|
+function $set (index, val) {
|
|
|
if (index >= this.length) {
|
|
|
this.length = index + 1
|
|
|
}
|
|
|
return this.splice(index, 1, val)[0]
|
|
|
-})
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* Convenience method to remove the element at given index.
|
|
|
@@ -105,13 +111,21 @@ _.define(arrayAugmentations, '$set', function (index, val) {
|
|
|
* @param {*} val
|
|
|
*/
|
|
|
|
|
|
-_.define(arrayAugmentations, '$remove', function (index) {
|
|
|
+function $remove (index) {
|
|
|
if (typeof index !== 'number') {
|
|
|
index = this.indexOf(index)
|
|
|
}
|
|
|
if (index > -1) {
|
|
|
return this.splice(index, 1)[0]
|
|
|
}
|
|
|
-})
|
|
|
+}
|
|
|
+
|
|
|
+if (_.hasProto) {
|
|
|
+ _.define(arrayAugmentations, '$set', $set)
|
|
|
+ _.define(arrayAugmentations, '$remove', $remove)
|
|
|
+} else {
|
|
|
+ arrayAugmentations.$set = $set
|
|
|
+ arrayAugmentations.$remove = $remove
|
|
|
+}
|
|
|
|
|
|
module.exports = arrayAugmentations
|