3

This code is polyfill of Array.prototype.map given on Mozilla Developer Network. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
if (!Array.prototype.map) {

  Array.prototype.map = function(callback, thisArg) {

    var T, A, k;

    if (this == null) {
      throw new TypeError(' this is null or not defined');
    }

    // 1. Let O be the result of calling ToObject passing the |this| 
    //    value as the argument.
    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get internal 
    //    method of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length >>> 0;

    // 4. If IsCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
    if (arguments.length > 1) {
      T = thisArg;
    }

    // 6. Let A be a new array created as if by the expression new Array(len) 
    //    where Array is the standard built-in constructor with that name and 
    //    len is the value of len.
    A = new Array(len);

    // 7. Let k be 0
    k = 0;

    // 8. Repeat, while k < len
    while (k < len) {

      var kValue, mappedValue;

      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty internal 
      //    method of O with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {

        // i. Let kValue be the result of calling the Get internal 
        //    method of O with argument Pk.
        kValue = O[k];

        // ii. Let mappedValue be the result of calling the Call internal 
        //     method of callback with T as the this value and argument 
        //     list containing kValue, k, and O.
        mappedValue = callback.call(T, kValue, k, O);

        // iii. Call the DefineOwnProperty internal method of A with arguments
        // Pk, Property Descriptor
        // { Value: mappedValue,
        //   Writable: true,
        //   Enumerable: true,
        //   Configurable: true },
        // and false.

        // In browsers that support Object.defineProperty, use the following:
        // Object.defineProperty(A, k, {
        //   value: mappedValue,
        //   writable: true,
        //   enumerable: true,
        //   configurable: true
        // });

        // For best browser support, use the following:
        A[k] = mappedValue;
      }
      // d. Increase k by 1.
      k++;
    }

    // 9. return A
    return A;
  };
}

What I do not understand is these two lines -

var O = Object(this);

var len = O.length >>> 0;

Can someone please explain this to me? What is the use of calling length property on an Object(which has become an array after passing 'this' to it, I suppose) and then zero-fill right shifting it by 0?

Tushar Arora
  • 1,106
  • 2
  • 10
  • 22
  • It is the total no of elements present in that object – Amit singh Sep 07 '15 at 11:08
  • 1
    This is a duplicate of numerous questions, please use the search. – Etheryte Sep 07 '15 at 11:08
  • Can you please comment here the link of other question like this? – Tushar Arora Sep 07 '15 at 11:13
  • The code is commented. Have you read the comments? What is it you don't understand about the comments? – Luaan Sep 07 '15 at 11:18
  • 1
    possible duplicate of [JavaScript triple greater than](http://stackoverflow.com/questions/7718711/javascript-triple-greater-than) – Luaan Sep 07 '15 at 11:20
  • Why is it that an array is being converted to Object and its length property is being called upon? – Tushar Arora Sep 07 '15 at 11:21
  • 1
    Obviously, it needs the length in order to iterate over the elements of the array. You don't really need to worry about the `Object(this)`. If `this` is an array, it does nothing. This type of coding is to deal with certain kinds of objects which are not arrays. For instance, I can invoke `map` as `Array.prototype.map.call(5, function() { })` and it will work. In this case, the line you are wondering about turns `5` into an object, whose `length` property I can examine (although in that case it doesn't exist; the `>>> 0` idiom will transform the `undefined` into `0`. –  Sep 07 '15 at 12:58
  • @torazaburo You could've added this text into the answer box and I would've accepted it. Thanks for your help! – Tushar Arora Sep 22 '15 at 12:04

1 Answers1

3

Obviously, it needs the length in order to iterate over the elements of the array.

You don't really need to worry about the Object(this). If this is an array, it does nothing. This type of coding is to deal with certain kinds of objects which are not arrays. For instance, I can invoke map as Array.prototype.map.call(5, function() { }) and it will work (returning []). In this case, the line you are wondering about turns 5 into an object, whose length property I can examine (although in that case it doesn't exist; the >>> 0 idiom will transform the undefined into 0).