Permalink
Browse files

Make array shims non-enumerable.

And give up on ES5 support.  defineProperty is required.
  • Loading branch information...
1 parent 2414fe3 commit db2689d26d7244861aac118d2a2b71a11bf56ab0 @kriskowal committed Nov 8, 2012
Showing with 78 additions and 266 deletions.
  1. +21 −4 listen/array-changes.js
  2. +0 −214 shim-array-es5.js
  3. +57 −47 shim-array.js
  4. +0 −1 shim.js
View
@@ -37,11 +37,28 @@ if (protoIsSupported) {
Object.defineProperties(this, observableArrayProperties);
};
}
-Array.prototype.makeObservable = array_makeObservable;
-Object.addEach(Array.prototype, PropertyChanges.prototype);
-Object.addEach(Array.prototype, RangeChanges.prototype);
-Object.addEach(Array.prototype, MapChanges.prototype);
+Object.defineProperty(Array.prototype, "makeObservable", {
+ value: array_makeObservable,
+ writable: true,
+ configurable: true,
+ enumerable: false
+});
+
+function defineEach(prototype) {
+ for (var name in prototype) {
+ Object.defineProperty(Array.prototype, name, {
+ value: prototype[name],
+ writable: true,
+ configurable: true,
+ enumerable: false
+ });
+ }
+}
+
+defineEach(PropertyChanges.prototype);
+defineEach(RangeChanges.prototype);
+defineEach(MapChanges.prototype);
var observableArrayProperties = {
View
@@ -1,214 +0,0 @@
-"use strict";
-
-/*
- Based on ES5-Shim
- Copyright (c) 2009-2012, Kris Kowal and others, MIT License
- http://github.com/kriskowal/es5-shim
-*/
-
-var GenericCollection = require("./generic-collection");
-
-// Having a toString local variable name breaks in Opera so use object_toString.
-var object_toString = Object.prototype.toString;
-
-// ES5 15.4.3.2
-// http://es5.github.com/#x15.4.3.2
-// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
-if (!Array.isArray) {
- Array.isArray = function (object) {
- return object_toString.call(object) == "[object Array]";
- };
-}
-
-// The IsCallable() check in the Array functions
-// has been replaced with a strict check on the
-// internal class of the object to trap cases where
-// the provided function was actually a regular
-// expression literal, which in V8 and
-// JavaScriptCore is a typeof "function". Only in
-// V8 are regular expression literals permitted as
-// reduce parameters, so it is desirable in the
-// general case for the shim to match the more
-// strict and common behavior of rejecting regular
-// expressions.
-
-// ES5 15.4.4.18
-// http://es5.github.com/#x15.4.4.18
-// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
-if (!Array.prototype.forEach) {
- Array.prototype.forEach = GenericCollection.forEach;
-}
-
-// ES5 15.4.4.19
-// http://es5.github.com/#x15.4.4.19
-// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
-if (!Array.prototype.map) {
- Array.prototype.map = GenericCollection.map;
-}
-
-// ES5 15.4.4.20
-// http://es5.github.com/#x15.4.4.20
-// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
-if (!Array.prototype.filter) {
- Array.prototype.filter = GenericCollection.filter;
-}
-
-// ES5 15.4.4.16
-// http://es5.github.com/#x15.4.4.16
-// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
-if (!Array.prototype.every) {
- Array.prototype.every = GenericCollection.every;
-}
-
-// ES5 15.4.4.17
-// http://es5.github.com/#x15.4.4.17
-// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
-if (!Array.prototype.some) {
- Array.prototype.some = GenericCollection.some;
-}
-
-// ES5 15.4.4.21
-// http://es5.github.com/#x15.4.4.21
-// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
-if (!Array.prototype.reduce) {
- Array.prototype.reduce = function reduce(callback /*, initial*/) {
- var self = toObject(this),
- length = self.length >>> 0;
-
- // If no callback function or if callback is not a callable function
- if (object_toString.call(callback) != "[object Function]") {
- throw new TypeError(callback + " is not a function");
- }
-
- // no value to return if no initial value and an empty array
- if (!length && arguments.length == 1) {
- throw new TypeError('reduce of empty array with no initial value');
- }
-
- var i = 0;
- var result;
- if (arguments.length >= 2) {
- result = arguments[1];
- } else {
- do {
- if (i in self) {
- result = self[i++];
- break;
- }
-
- // if array contains no values, no initial value to return
- if (++i >= length) {
- throw new TypeError('reduce of empty array with no initial value');
- }
- } while (true);
- }
-
- for (; i < length; i++) {
- if (i in self) {
- result = callback.call(void 0, result, self[i], i, self);
- }
- }
-
- return result;
- };
-}
-
-// ES5 15.4.4.22
-// http://es5.github.com/#x15.4.4.22
-// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
-if (!Array.prototype.reduceRight) {
- Array.prototype.reduceRight = function reduceRight(callback /*, initial*/) {
- var self = toObject(this),
- length = self.length >>> 0;
-
- // If no callback function or if callback is not a callable function
- if (object_toString.call(callback) != "[object Function]") {
- throw new TypeError(callback + " is not a function");
- }
-
- // no value to return if no initial value, empty array
- if (!length && arguments.length == 1) {
- throw new TypeError('reduceRight of empty array with no initial value');
- }
-
- var result, i = length - 1;
- if (arguments.length >= 2) {
- result = arguments[1];
- } else {
- do {
- if (i in self) {
- result = self[i--];
- break;
- }
-
- // if array contains no values, no initial value to return
- if (--i < 0) {
- throw new TypeError('reduceRight of empty array with no initial value');
- }
- } while (true);
- }
-
- do {
- if (i in this) {
- result = callback.call(void 0, result, self[i], i, self);
- }
- } while (i--);
-
- return result;
- };
-}
-
-// ES5 15.4.4.14
-// http://es5.github.com/#x15.4.4.14
-// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
-if (!Array.prototype.indexOf) {
- Array.prototype.indexOf = function indexOf(sought /*, fromIndex */ ) {
- var self = toObject(this),
- length = self.length >>> 0;
-
- if (!length) {
- return -1;
- }
-
- var i = 0;
- if (arguments.length > 1) {
- i = toInteger(arguments[1]);
- }
-
- // handle negative indices
- i = i >= 0 ? i : Math.max(0, length + i);
- for (; i < length; i++) {
- if (i in self && self[i] === sought) {
- return i;
- }
- }
- return -1;
- };
-}
-
-// ES5 15.4.4.15
-// http://es5.github.com/#x15.4.4.15
-// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
-if (!Array.prototype.lastIndexOf) {
- Array.prototype.lastIndexOf = function lastIndexOf(sought /*, fromIndex */) {
- var self = toObject(this),
- length = self.length >>> 0;
-
- if (!length) {
- return -1;
- }
- var i = length - 1;
- if (arguments.length > 1) {
- i = Math.min(i, toInteger(arguments[1]));
- }
- // handle negative indices
- i = i >= 0 ? i : length - Math.abs(i);
- for (; i >= 0; i--) {
- if (i in self && sought === self[i]) {
- return i;
- }
- }
- return -1;
- };
-}
-
Oops, something went wrong.

0 comments on commit db2689d

Please sign in to comment.