0

Hello I've come across this piece of code and I have no idea how it works.

function firstFunction(something, else){
     //stuff being done
     return something;
}

var myFunction = firstFunction(function(a,b){
    return a*b;
},'car');

So I am fairly new to Javascript so I'll just say it is, a really confusing language. I understand I am declaring a variable that is assigning the firstFunction to itself and passing some noname function as first parameter and a String as a second one.

How do I go about passing the arguments into that noname functions?

John Smith
  • 87
  • 10
  • What do you want to pass where? Do you want to pass `'car'` to the anonymous function? – Felix Kling Apr 11 '14 at 21:43
  • possible duplicate of [Create a custom callback in JavaScript](http://stackoverflow.com/questions/2190850/create-a-custom-callback-in-javascript) – Felix Kling Apr 11 '14 at 21:45
  • I want to pass values into the function(a,b). I know I can do that using myFunction.apply(this, arguments) however I don't fully understand as what is 'this' applied to and where it is used. – John Smith Apr 11 '14 at 21:45
  • 2
    First of all the right term is **anonymous function**. Secondly, the the anon function being passed is probably called in the 'stuff being done' section of `firstFunction`. So you need to give us more really. – Sanketh Apr 11 '14 at 21:45
  • `this` refers to the global object (`window`), since you are calling the function "normally", like `foo()`. How `this` works is extensively explained in the MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this. – Felix Kling Apr 11 '14 at 21:46
  • @FelixKling No 'car' is already passed as the second parameter to the firstFunction, I want to pass the paramaters into the anonymous function like someone has already answered. The link you provided is not working, as for 'this' I have read about it, but when I call myFunction.apply(this, arguments) I can't be refering to the window object in that situation can i? – John Smith Apr 11 '14 at 21:52
  • *"I want to pass the paramaters into the anonymous function"* Which parameters? Did you really only want to know how to call the function that is in `myFunction`? – Felix Kling Apr 11 '14 at 21:53
  • @FelixKling "Did you really only want to know how to call the function that is in myFunction" yes, yes I did as I mentioned I am fairly new to JS. Should I edit this question or should I make a new thread on function.apply and this question? – John Smith Apr 11 '14 at 22:06
  • I don't see how `apply` has anything to do with that. Your question would be clearer if you had written something like "I know `myFunction` contains a function, how do I call it?". And FWIW, all functions can be called in the same ways, not matter how they were defined/where they are coming from. – Felix Kling Apr 11 '14 at 22:13
  • I did that but in other words, sorry if I wasn't clear enough. As for apply, I am curious as to what 'object' is 'this' binded in this function calling myFunction.apply(this, arguments); I understand the usage of apply and when the regular object is passed in place of this. – John Smith Apr 11 '14 at 22:20
  • When you call `myFunction.apply(this, arguments)`, the `this` *inside* the function is bound to `this` *outside* function. And if you call it in global scope, then `this` refers to `window`. – Felix Kling Apr 12 '14 at 01:22

1 Answers1

0

Yes, your assessment is pretty much correct, although the code has a syntax error because you can use else as an identifier. These are referred to as anonymous functions.

Function in JavaScript can be used as parameter values or return values just as easily as any other value. This is what makes them first-class citizens of the language.

The anonymous function you pass in to firstFunction is returned and assigned to myFunction so if you want to call it, you can simply do this:

var result = myFunction(4, 5); // 20
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331