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.

2

4 Answers 4

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/

Sign up to request clarification or add additional context in comments.

6 Comments

okay so it's just passing a function as a parameter basically
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.
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?
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.
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?
|
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.

3 Comments

Oh okay, thanks for relating it to C, that's kinda what I base my programming knowledge off of, So these would be the same: param1 = function1(); function2(int x, param1); function2(int x, function1( define the function in here));
Wait, C# can do that too? How exactly is that?
See C#'s delegates
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.