If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

*SOMEWHERE* there has to be a loop through the elements. Granted, with filter() the loop is in native code, not JS, but then the call to the filter method for *each* element has to be tons slower than a simple loop in JS code. I would bet it would be an order of magnitude slower, in fact.

An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

I started out trying to figure out the .filter() function,
but have abandoned that because I thought I understood your Array.prototype function.

Now I don't understand what's happening with your latest modification with the 'callback' addition.

Here is the test code I am using to try to understand the working.
In the comment section at the end, I list what I expected and what is displayed.
??? indicates that I don't understand the results.

If you have the time, now that you have fogged my prior clarity, could you explain the
difference between what I thought I know and what is really happening?
(I may be testing your mind reading skills with that last statement).

First of all, understand this: Whatever the callback function *RETURNS* for each element it is called for is what ends up in the OUTPUT array. Anything the function does to the *ORIGINAL* array has *NO IMPACT AT ALL* on what happens to the output array.

SO;

Code:

var Barr = Aarr.forEach( function(element) { return element + 1; } )

You are not changing any elements of the Aaar array; you are returning each element + 1. So each element of Barr will be one greater than Aarr.

You are changing each element of the input array. But you are returning the *original* element value. So the output array (BBarr) will get a copy of the original array while, at the same time, each element of the original array is incremented by 1.

Remember: the value of element *IS* the ORIGINAL value of the element. Changing the element value inside the current (thiis) array does *NOT* impact the value of element.

Maybe if we look at the forEach method it will be clearer. It truly is simple.

First of all, let me simplify it to *require* that the callback function be supplied:

Code:

Array.prototype.forEach = function( callback ) {
// we build the new array here:
var ar = [];
// we loop through every element of the old array:
for ( var i = 0; i < this.length; ++i ) {
// and we put the RESULT of calling the callback function into
// the corresponding element of the new array:
ar[i] = callback( this[i], this, i );
}
// finally returning the new array:
return ar;
}

So the only thing to really need to understand is this line:

Code:

ar[i] = callback( this[i], this, i );

The assignment into the new array is hopefully obvious. So let's look at *HOW* the callback function is called:

Code:

callback(
this[i], /* the *VALUE* of the current (i-th) element of the array */
this, /* the *ENTIRE* current array */
i /* the element number */
);

So you can see that in your callback function:

Code:

// using one of your example:
function(element, array, index)
{
// remember: element is the *VALUE* of array[index]
// but it was *ALREADY* fetched out of the array in the forEach method/function
// so when we do this, we are indeed changing the element number index in the
// original array...
array[index] += 1;
// but that in no way affects the value of element:
return element;
}

Did you perhaps forget that arrays are passed to functions *BY REFERENCE*? So the variable array in your callback function is 100% the same *OBJECT* as the array referred to by this in the forEach method.

Clearer?

An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.