Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Basics (1.x) Services in Angular Using Services To Get Data

Kirsty Pollock
Kirsty Pollock
14,261 Points

How do we know that the callback function needs a "response" argument?

I am very experienced in other languages, but not js. I "get" callbacks. What I don't get in this video is how we know the callback declaration should have the "response" argument. i.e. have the form:

function(response) { ...

I guess the issue is that I am used to strong type declarations ... here I don't know where to look for what the callback declaration should conform to.

Is the only way just to look at the relevant jsAngular documentation? (i.e. https://docs.angularjs.org/api/ng/service/$http and look for an example, but I was expecting somewhere an explicit description of exactly what the callback can/should look like).

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hey Kirsty,

Unfortunately, in the JavaScript world without a transpiler like TypeScript and definition files, documentation is the only means of discovery. Luckily, Angular 2 is built with TypeScript, so that's something to look forward to :)

I use a tool called devdocs that makes searching across many libraries and frameworks quick and easy. Here's the documentation for $http in Angular 1.5, for example.

I hope this helps.

Kirsty Pollock
Kirsty Pollock
14,261 Points

devdocs looks very useful - thanks! I am intending to get into TypeScript next...!

Justin Horner
Justin Horner
Treehouse Guest Teacher

You're welcome! TypeScript is awesome, I think you'll love it :)

Justin's hit the nail on the head here. The 'fun' thing with JS is that you can 'overload' function calls. For example, say your function signature is this:

function myFunc (arg1) {
 // do something with my arg1
}

You would still be able to overload the function, even if you're not explicitly asking for a second argument:

myFunc('hello', 'world');

(Even if in this case you're not doing anything with the second function)

This makes it useful for currying and extending, and particularly for making vanity methods:

function concatString(){
  return Array.from(arguments).join('-');
}
concatString('a', 'b', 'c'); // -> 'a-b-c';

Or to overload (based on Steve Jansen's example) :

const items = [
  {
    firstName: 'Stuart',
    lastName: 'Elmore'
  },
  {
    firstName: 'Samuel',
    middleName: 'L',
    lastName: 'Jackson'
  },
  {
    firstName: 'Samuel',
    lastName: 'Pepys'
  }
];

function getAllItems(){
  return items;
}

function getByFirstName(firstName){
  return items.filter(function(item){
    return item.firstName === firstName;
  });
}

function getByFullName(name){
  return items.filter(function(item){
    return ( item.firstName + ' ' + item.lastName ) === name;
  });
}

function get(){
  if(arguments.length === 0){
    return getAllItems();
  }else if(arguments.length === 1){
    return getByFirstName.apply(this, arguments);
  }else{
    return getByFullName.apply(this, arguments);
  }
}

get(); // -> returns all items
get('Samuel'); /* -> 
  [
    { firstName: 'Samuel', middleName: 'L', lastName: 'Jackson' }, 
    { firstName: 'Samuel', lastName: 'Pepys' }
  ]
*/
get('Stuart', 'Elmore'); // -> { firstName: 'Stuart', lastName: 'Elmore' }
Kirsty Pollock
Kirsty Pollock
14,261 Points

cool. I was wondering how to do overloads...