3

I come from a background in C/C#/Java and PHP so I'm used to those standards of coding, where a function is defined using function(parameters) { ... return x}

But lately I've been learning some JS libraries like Angular/Node and come across functions (or maybe there not?) with another function inside the parameters, like this:

app.controller('MainController', ['$scope', 'forecast', function($scope,     forecast) {
forecast.success(function(data) {
$scope.fiveDay = data;
});
}]);

app.factory('forecast', ['$http', function($http) { 
return $http.get('https://s3.amazonaws.com/codecademy-    content/courses/ltp4/forecast-api/forecast.json') 
        .success(function(data) { 
          return data; 
        }) 
        .error(function(err) { 
          return err; 
        }); 
 }]);

It just confuses me and goes against what I've learned about functions. Is this convention only used in scripting languages? I seems like they go

function(parameter, function(parameter) { do something; }); 

Anyone explain why this is used or if it does anything than a normal function?

Thanks.

JimmySmithJR
  • 301
  • 1
  • 9
  • 17
  • that's an anonymous function, defined and passed -as a parameter- to another function at the same time. – Aᴍɪʀ Jan 05 '16 at 00:30
  • Possible duplicate of [Getting a better understanding of callback functions in JavaScript](http://stackoverflow.com/questions/483073/getting-a-better-understanding-of-callback-functions-in-javascript) – Aᴍɪʀ Jan 05 '16 at 00:32

4 Answers4

1

In Javascript a variable can return a function, which could return another function, pretty much endlessly.

For example:

var myFunction = function() {
    return getAnswer();
}

var getAnswer = function() {
    return "Hello world!";
}

console.log(myFunction); will return

var myFunction = function() { return getAnswer(); }

and console.log(myFunction()); will return

"Hello world!"

So App.controller is a variable that is part of the app object, but it's a function so you are passing in parameters, some of which can be a function.

https://jsfiddle.net/Lu46sf2v/2/

Justin
  • 2,265
  • 4
  • 15
  • 21
  • okay so it's just passing a function as a parameter basically – JimmySmithJR Jan 05 '16 at 00:47
  • I don't quite think I understand your question? param1= function1() if that's what you set it as. In your example above, angular uses the injection to connect your controller to the views and other JS files like your factory/services, etc. – Justin Jan 05 '16 at 00:51
  • Sorry I meant to delete that comment so in javascript you can define a function while it's passed inside as a parameter, meaning you don't have to define it elsewhere, you can just put the definition right there in the args? And app.controller, and app.factory, even though they have those long definitions, they are still considered functions of app? – JimmySmithJR Jan 05 '16 at 00:54
  • Yeah I think you are on the right track. You can think of 'app' as an object. So `var app = { controller: function() { }, factory: function() {} }` Obviously it's a bit more complicated than that, but at a basic level that is what's happening. – Justin Jan 05 '16 at 01:03
  • okay so in factory, for it's second parameter, if you defined the whole function($http) { .. } in another variable like var a = function($http) {...}, could that original code i posted be replaced with app.factory('forecast', ['$http', a]), because a is equal to the function, instead of writing out the entire thing? – JimmySmithJR Jan 05 '16 at 01:09
  • Yes you should be able to. Since anything can be a variable in Javascript you could write out the function or you can pass it in as a variable. It _should_ work the same. – Justin Jan 05 '16 at 01:16
0

Is a way of passing a function as an argument to another function. In C, you can define a function, and then pass it to another function as a pointer. In Javascript, you can just define the function right there. C# can do that too.

Cecilio Pardo
  • 1,717
  • 10
  • 13
0

In javascript, functions are first-class objects, meaning you can attach parameters to them, rename them, create them on the fly, or pass them like normal objects.

In node.js, for instance, most libraries specify a function to call after some operation completes. You can either point it to an existing function by name, or you can make an anonymous one and stuff it into the parameters.

John Kossa
  • 459
  • 2
  • 8
0

In Javascript, functions are first-class functions, this means you can pass a function as an argument, or return a new function inside a function.

What you're seeing is a callback, you receive that function as an argument and execute sometime later when you need.

E.g: A function that will be called when you click at a button

var btn = document.querySelector('btn');

btn.addEventListener('click', function() {
    alert("I was clicked");
});

This code will add a listener to the button, when the click is fired it'll call the callback function.

Ian Ramos
  • 414
  • 7
  • 16