Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-14-2010, 01:08 AM   PM User | #1
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
the filter method of array

Hey all,

Quick question. I was reading this book art/science of javascript and it defines its own custom filter method:

Code:
if (!Array.prototype.filter) { 
	Array.prototype.filter = function(func, scope) { 
	scope = scope || this; 
	var list = []; 
	for (var i = 0, l = this.length; i < l; i++) 
	if (func.call(scope, this[i], i, this)) list.push(this[i]); 
	return list; 
	} 
}
I honestly don't understand this line right here "if (func.call(scope, this[i], i, this)) list.push(this[i]); " and what contribtues to that is that the book doesn't provide an example of the filter method being used. So I was wondering if anyone can provide a practical example of how to use this method.

Thanks for any response.
johnmerlino is offline   Reply With Quote
Old 06-14-2010, 05:09 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Explanation of Array.filter() @ MDC
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 06-14-2010, 06:38 PM   PM User | #3
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
I found another similar example, but notice how the call is sending for arguments: fun.call(thisp, this[i], i, this);
Yet the function (which is invoked by call()) is only receiving three arguments:
printBr(element, index, array)

How is this possible?

Also, why would we be passing the second index of the array right here: var thisp = arguments[1];
I don't see the point of that.

Code:
<html>
<head>
<title>JavaScript Array forEach Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function printBr(element, index, array) {
  document.write("<br />[" + index + "] is " + element ); 
}

[12, 5, 8, 130, 44].forEach(printBr);
  
</script>
</body>
</html>

Last edited by johnmerlino; 06-14-2010 at 06:42 PM..
johnmerlino is offline   Reply With Quote
Old 06-15-2010, 09:33 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
do you know how the Function.call() function works?

parameters 2 to 4 are passed to the function called, parameter 1 is used by .call() itself.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 06-15-2010, 08:57 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by johnmerlino View Post

Also, why would we be passing the second index of the array right here: var thisp = arguments[1];
I don't see the point of that.
you aren't. you are specifing what this means in the 2nd argument to filter.


for example, we can use "this" as an extra argument to help create generic functions:


Code:
function gt(a){ return a > this ; }

var r=[1,2,3,4,5,6];


alert( r.filter(  gt,  3  ));
in this example, the array r is just some numbers.
the comparator function gt returns true if it's this is less than it's argument.

by mapping gt to the array in a filter method, we remove all elements whose value is less than this.

when i set this to 3, (by passing a 2nd arg to filter), the alert shows "4,5,6"; the numbers that are greater than 3.


does that make sense?
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
johnmerlino (06-17-2010)
Old 06-17-2010, 02:31 AM   PM User | #6
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
So basically:

i = to the index number
this[i] = equal to the integer or DOM element
this = equal to the array
pthis = equal to a specific integer or DOM element in array depending on what we passed from the arguments method - arguments[1] will pass the second index value.

Is this correct?
Thanks.
johnmerlino is offline   Reply With Quote
Old 06-17-2010, 07:23 AM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
thisp is the object that this inside fun will point to.
this[i] is the i(th) Array element/item

if you don’t declare the thisp parameter in the function call (it is commented out in the example), you have to get it another way, that is by using the (internal) variable arguments holding the parameters passed to the function.
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 06-17-2010 at 07:28 AM..
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
johnmerlino (06-18-2010)
Reply

Bookmarks

Tags
array, filter, javascript, prototype, scope

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:31 AM.


Advertisement
Log in to turn off these ads.