Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 16 of 16
  1. #16
    New Coder
    Join Date
    Aug 2013
    Posts
    50
    Thanks
    0
    Thanked 11 Times in 11 Posts
    Array.map and [].map are not the same function.
    Test it in your console... Array.map === [].map; // false

    Generic Array.map takes 3 arguments:
    Code:
    Array.map( arrayLikeObject, fn [, otherThis] );
    You have to provide the arrayLikeObject as the first argument of the function, unlike the following method.



    An instance of an array's map method takes only 2 arguments, since it uses 'this' for the arrayLikeObject.
    Code:
    [].map.call(arrayLikeObject, fn [, otherThis ] );
    I know it looks like you're passing 3 arguments, but you're actually calling [].map with 2 arguments and a custom 'this' value, not 3 arguments as in the generic map function.

    The above would be the same as doing this
    Code:
    arrayLikeObject.map( fn [, otherThis ] ); // 2 arguments. 'this' is 'arrayLikeObject'
    Also note that [].map is just a shortcut for Array.prototype.map
    Code:
    Array.prototype.map === [].map; // true
    Array.prototype.map === [].constructor.prototype.map; // true

    ------------------------------------------------------------


    @Dormillich: This is what it needs to be to work properly:
    Code:
    [].map.call(document.querySelectorAll("input[type='text']"), function(elm){
       elm.onkeyup= function() {
        submit.disabled= !(first_name.value && second_name.value)
       };
    });
    Or... the equivalent:
    Code:
    Array.map(document.querySelectorAll("input[type='text']"), function(elm){
       elm.onkeyup= function() {
        submit.disabled= !(first_name.value && second_name.value)
       };
    });
    Note the difference. The former method is executing the .call() function where as the latter method is executing the .map() function.
    Last edited by joesimmons; 09-29-2013 at 01:16 AM.

  2. Users who have thanked joesimmons for this post:

    jason_kelly (09-30-2013)


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •