Gatene Gatene - 2 years ago 108
Javascript Question

function creation as a parameter?

I have tried asking this on specialty forums, and no one answers this for me, But I am hoping someone here can. Below is the snippet of code in question, and I know how it all works except for 2 bits of it. = allItems().filter(function(item) {
return this.includes(item);
}, this);

As stated above, I understand all but two bits of the code above: What is this type of function called? a call back? There is no function called "item" or a variable within the main function called "item". Lastly, why is "this" a part of the snippet?

Sorry if this isn't enough information, I am trying to be as thorough as I can with this question as I am not familiar with this style of scripting. I have done some research on this subject, and the closest explanation as I can get is a private function, but as I mentioned above, there is no function called "item" anywhere.

Answer Source

Javascript is a bit of a strange language. For those of us that learned a compiled programming language first, it shares a visual aesthetic but the behind-the-scenes are very different.The first thing to understand is that in javascript every function as a special reference called this. It references the current iteration of the function.

Think of it like this: there is a class called Human, which defines a set of properties common to all humans -- we have a height and weight, a name, eye color, hair color, etc. But me, being 5'8", 200 lbs, hazel eyed, and bald is not the same description as you, or any other human being. While there could definitely be another human with those characteristics, we are not the same.

var Human = function(name, height, weight, eye_color) { = name;
  this.height = height;
  this.weight = weight;
  this.eye_color = eye_color;

var me = new Human('Jhecht', '5.8', 200, 'hazel');
alert(; //Alerts 'Jhecht'
var you = new Human('Gatene', '6.0', 190, 'brown');
alert(; //Alerts 'Gatene'

The this in the code ensures that the variables me and you mark us as individuals. This way, even though we can both have things in common, it is understood that we are not the same. It also makes it so that you can check on that information later. Without the this I would just be setting each variable equal to itself (name=name) which gives me no way to look at its contents later. If I removed the and replaced it with name, I would no longer get my expected result if I tried to call alert( would most likely get an undefined value) When used on event listeners for HTML elements, this will refer to the HTML element, unless you tell it otherwise.

Functions in javascript are also a little less rigid (and I don't feel like that's the correct word, but it's the best one I've got) than functions in other languages. Notice in my code above I set my function like so:

var Human = function(name, height, weight, eye_color) ...

I could have also created that function like so:

function Human(name, height, weight, eye_color)...

and the outcome would be the same.

So in your code snippet, let's go through line by line. = allItems().filter(function(item) {
    return this.includes(item);
}, this);
  1. is saying "the data variable on this instance of the function call this code is from" is equal to allItems() which presumably returns an array of items, is filtered down by a function. Since Javascript allows for something called 'anonymous' functions, aka functions without a name, we create an anonymous function (non-named function) which accepts a named argument of item.
  2. the function being passed to the .filter method in line 1 will return the result of the includes method (most likely defined somewhere previously in your code), which was being passed the value of our argument from line 1, item.
  3. Ends the function definition with }. the next portion goes back to what I said before, this refers to the current function call, unless you instruct it not to. the , this); is the programmer saying "on this function, which I declared above, I want this to refer to the object I am using currently."

This part is tricky, and it will definitely take some time to understand so if you're not entirely sure don't worry too much about it.

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download