- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayLikeIsArray.js
More file actions
Latest commit
47 lines (44 loc) · 1.29 KB
/
Copy pathArrayLikeIsArray.js
File metadata and controls
47 lines (44 loc) · 1.29 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
/**
* Provides ArrayLike support for for libraries that verify an object
* is an Array via either Object.prototype.toString or Array.isArray.
*
* For more information: https://github.com/dribnet/ArrayLike.js
*/
// this method patches (or implements) Array.isArray
// and stores the original function so it can be restored
(function(ar){
varisArray=ar.isArray;
// toString is only used if Array.isArray does not exist
vartoString=Object.prototype.toString;
// ar["$originalIsArray"] = isArray;
ar.isArray=function(obj){
varthat=this;
if(obj&&obj.__ArrayLike){
returntrue;
}
elseif(isArray){
returnisArray.call(that,obj);
}
else{
// fallback - polyfill the Array.isArray method
returntoString.call(obj)==="[object Array]";
}
}
// also install isArrayLike, but here it matches isArray
ar.isArrayLike=ar.isArray;
})(Array);
// this method patches Object.prototype.toString
// and stores the original function so it can be restored
(function(op){
vartoString=op.toString;
// op["$originalToString"] = toString;
op.toString=function(args){
varthat=this;
if(that&&that.__ArrayLike){
return'[object Array]';
}
else{
returntoString.call(that);
}
}
})(Object.prototype);