1+ /*
2+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
3+ */
4+ // Production steps of ECMA-262, Edition 5, 15.4.4.19
5+ // Reference: http://es5.github.io/#x15.4.4.19
6+ if ( ! Array . prototype . map ) {
7+
8+ Array . prototype . map = function ( callback , thisArg ) {
9+
10+ var T , A , k ;
11+
12+ if ( this === void 0 || this === null ) {
13+ throw new TypeError ( 'Array.prototype.map called on null or undefined' ) ;
14+ }
15+
16+ // 1. Let O be the result of calling ToObject passing the |this|
17+ // value as the argument.
18+ var O = Object ( this ) ;
19+
20+ // 2. Let lenValue be the result of calling the Get internal
21+ // method of O with the argument "length".
22+ // 3. Let len be ToUint32(lenValue).
23+ var len = O . length >>> 0 ;
24+
25+ // 4. If IsCallable(callback) is false, throw a TypeError exception.
26+ // See: http://es5.github.com/#x9.11
27+ if ( callback . __class__ !== 'Function' ) {
28+ throw new TypeError ( callback + ' is not a function' ) ;
29+ }
30+
31+ // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
32+ T = ( arguments . length > 1 ) ? thisArg : void 0 ;
33+
34+ // 6. Let A be a new array created as if by the expression new Array(len)
35+ // where Array is the standard built-in constructor with that name and
36+ // len is the value of len.
37+ A = new Array ( len ) ;
38+
39+ for ( var k = 0 ; k < len ; k ++ ) {
40+
41+ var kValue , mappedValue ;
42+
43+ // a. Let Pk be ToString(k).
44+ // This is implicit for LHS operands of the in operator
45+ // b. Let kPresent be the result of calling the HasProperty internal
46+ // method of O with argument Pk.
47+ // This step can be combined with c
48+ // c. If kPresent is true, then
49+ if ( k in O ) {
50+
51+ // i. Let kValue be the result of calling the Get internal
52+ // method of O with argument Pk.
53+ kValue = O [ k ] ;
54+
55+ // ii. Let mappedValue be the result of calling the Call internal
56+ // method of callback with T as the this value and argument
57+ // list containing kValue, k, and O.
58+ mappedValue = callback . call ( T , kValue , k , O ) ;
59+
60+ // iii. Call the DefineOwnProperty internal method of A with arguments
61+ // Pk, Property Descriptor
62+ // { Value: mappedValue,
63+ // Writable: true,
64+ // Enumerable: true,
65+ // Configurable: true },
66+ // and false.
67+
68+ // In browsers that support Object.defineProperty, use the following:
69+ // Object.defineProperty(A, k, {
70+ // value: mappedValue,
71+ // writable: true,
72+ // enumerable: true,
73+ // configurable: true
74+ // });
75+
76+ // For best browser support, use the following:
77+ A [ k ] = mappedValue ;
78+ }
79+ }
80+ // 9. return A
81+ return A ;
82+ } ;
83+ }
0 commit comments