function as a parameter

Discussion in 'Learning Javascript' started by Gatene, May 4, 2016.

  1. Gatene

    Gatene Intermediate Scripter Member

    Messages:
    11
    Likes Received:
    1
    Location:
    South Carolina
    First Language:
    English
    Hello all,


      I have two code examples below that describe my confusion, and questions about each example:


    1)   this._data = $gameParty.allItems().filter(function(item) 



    Code:
    2)   $gameParty.members().forEach(function(actor) {
              ImageManager.loadFace(actor.faceName());
           }, this);



    Okay, #1, I get that $gameparty.allitems concatenates the various item arrays to make one large array, and the .filter sifts through the array...but what in the bleep is the function being called there mean? My confusion stems from testing the code with alert(item); and getting NULL as a response or in some cases with similar code some error.


    #2, I understand that for each member of the party, some code needs to execute, but like above, actor doesn't exist as a variable...how can a function be passed a parameter that doesn't exist? Furthermore, what is "this" at the end of the function declaration?


    I am not familiar with this style of programming, and all the research I do leaves me with more questions than I had before; so I am at y'all's mercy.  My main questions about both snippets above is what is that function reference called? How can a parameter be sent to a function that doesn't exist? and why is "this" in there? is it binding something to this particular function?


    Thank you for your help, and I apologize in advance if these questions have come up in the past.


    Gatene
     
    #1
  2. Gatene

    Gatene Intermediate Scripter Member

    Messages:
    11
    Likes Received:
    1
    Location:
    South Carolina
    First Language:
    English
    Well, I got my answer from another forum....the function in both examples is known as an anonymous function. It is a function that can be unnamed because it is attached to something else: The first code snippet, the function is a parameter in the filter method, and in the second snippet it is the parameter in the foreach loop. anonymous functions can also be attached to variables.


    What, as I found out, was giving me so much problem was the item parameter in the anonymous function in the first snippet, and the actor parameter in the second. Come to find out, in the first snippet, item is simply a new variable created to hold the current element in the array that the function $gameParty.allItems produces. Similarly, the actor parameter in the second snippet holds the current value of the array created by the $gameParty.members() function.


    The last thing I was having trouble was the this attached at the end of the anonymous function. From what I understand is that it is the iterator that makes the variables work as I explained.


    Hope I've helped someone with this explanation,


    Gatene
     
    #2
  3. Tsukihime

    Tsukihime Veteran Veteran

    Messages:
    8,230
    Likes Received:
    3,067
    Location:
    Toronto
    First Language:
    English
    @Gatene


    In javascript, functions are just objects. For example,


    function add(a, b) {
    return a + b
    }


    This creates a function called `add`, which takes two parameters and returns the sum of them.


    In javascript, you can pass functions as arguments.


    Functions can be called at anytime, by anyone, in any place.


    Functions can call other functions.


    For example, the filter method takes a function that does something. Presumably, it iterates over the collection and passes each element to your function, which you would operate on.


    The filter method's ONLY job is to determine whether the element should be kept or not.


    The provided function's job is to explain HOW it is determined. The whole point of doing this is to save you time from having to rewrite the filter method yourself, and it also separates the responsibilities into two different methods rather than having to write a custom filter method everytime for any arbitrary condition you might have.


    As for `this`, recall that a function can be called by other functions. Each function has its own scope, and "this" inside the function, by default, would be checking its own scope, and NOT the caller's scope. By providing "this", you are telling the function to use the caller's scope.


    This is necessary when you are using methods like filter and whatever because you probably WANT it to be evaluated in the caller's scope.
     
    Last edited by a moderator: May 8, 2016
    #3
    Gatene likes this.
  4. Gatene

    Gatene Intermediate Scripter Member

    Messages:
    11
    Likes Received:
    1
    Location:
    South Carolina
    First Language:
    English
    Thanks Tsukihime,


      I had no idea that Javascript considered functions as objects. Not knowing about anonymous functions got me all confused, and the coders using "item" as the iterator confused me too...to me, "item" means the actrual item object (because I was working in the window_itemList callback function) or a menu item in a menu.


      As for "this", I will do more hunting around for examples and stuff, but to me "scope" means a line (or area) of sight. Thanks to you, Tsukihime, I understand that having "this" outside the curly bracket is evaluated as the caller's scope, but having "this" inside the curly bracket evaluates as the function's scope. I just don't know what "scope" means, heh.


    Thanks again,


    Gatene
     
    Last edited by a moderator: May 8, 2016
    #4

Share This Page