-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsubarray.js
More file actions
63 lines (54 loc) · 1.46 KB
/
subarray.js
File metadata and controls
63 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var makeSubArray = (function(){
var MAX_SIGNED_INT_VALUE = Math.pow(2, 32) - 1,
hasOwnProperty = Object.prototype.hasOwnProperty;
function ToUint32(value) {
return value >>> 0;
}
function getMaxIndexProperty(object) {
var maxIndex = -1, isValidProperty;
for (var prop in object) {
isValidProperty = (
String(ToUint32(prop)) === prop &&
ToUint32(prop) !== MAX_SIGNED_INT_VALUE &&
hasOwnProperty.call(object, prop));
if (isValidProperty && prop > maxIndex) {
maxIndex = prop;
}
}
return maxIndex;
}
return function(methods) {
var length = 0;
methods = methods || { };
methods.length = {
get: function() {
var maxIndexProperty = +getMaxIndexProperty(this);
return Math.max(length, maxIndexProperty + 1);
},
set: function(value) {
var constrainedValue = ToUint32(value);
if (constrainedValue !== +value) {
throw new RangeError();
}
for (var i = constrainedValue, len = this.length; i < len; i++) {
delete this[i];
}
length = constrainedValue;
}
};
methods.toString = {
value: Array.prototype.join
};
return Object.create(Array.prototype, methods);
};
})();
function SubArray() {
var arr = makeSubArray();
if (arguments.length === 1) {
arr.length = arguments[0];
}
else {
arr.push.apply(arr, arguments);
}
return arr;
}