2

I'm learning AngularJS, and someone wrote this code:

.factory('cribs',function(){
    var data = [{
        name: "jack",
        last: 'doe'
    },{
        name: 'hazel',
        last: 'man'
    }
    ];

    function getcrib(){
        return data;
    }
    return{
        getcrib: getcrib // what the heck does this mean?
    }
})

I'm baffled with the return line... he returned an object of getcrib with a function that returns the data? Does the getcrib at the beginning need to be the same exact case?

5
  • 1
    The semicolon is wrong, but yes, getcrib is a reference to the function declared in the lines above. The property name getcrib only matters to the user of the factory. Commented Jan 13, 2017 at 2:45
  • oh yes semi colon is wrong. could nt he just return the function itself instead of that? is this line of code similar? var t = function getcrib(){ ..} return t as such? Commented Jan 13, 2017 at 2:46
  • 2
    It doesn't matter in this tiny example, but when things get more complicated, by having a single place where you can see: 'return {usefulFunction: usefulFunction, anotherUsefulFunction: anotherUsefulFunction, ..., etc. }', it helps a person get a quick look at what gets 'exported' by the factory without having to wade through a ton of function declarations. Commented Jan 13, 2017 at 2:59
  • 1
    Yes, returning a function directly instead of returning an object with a single method is often a good idea, however you loose the method name which might or might not be significant. You'll want to ask the author of the code why he chose this approach, we cannot know. Commented Jan 13, 2017 at 3:02
  • Thank you very much guys!!! :D Commented Jan 13, 2017 at 3:22

1 Answer 1

3

All the four codes below does the same thing. Hope you have a better understanding on function declarations. :)

Code 1:

.factory('cribs',function(){
    var data = 3.142;
    function getcrib(){
        return data;
    }
    return{
        getcrib: getcrib 
    }
})
//console.log(cribs.getcrib()) outputs 3.142

Explanation:

  1. An object is returned.
  2. This object has a property of getcrib, which reference the function whose name is also getcrib.

Code 2:

.factory('cribs', function() {
  var data = 3.142;
  return {
    getcrib: function() {
      return data;
    }
  }
})
//console.log(cribs.getcrib()) outputs 3.142

Explanation:

  1. An object is returned.
  2. This object has a property of getcrib, which reference an anonymous function. (anonymous is a function without a name)

Code 3:

.factory('cribs',function(){
    var data = 3.142;
    function GET_PI_VALUE(){
        return data;
    }
    return{
        getcrib: GET_PI_VALUE
    }
})
//console.log(cribs.getcrib()) outputs 3.142

Explanation:

  1. An object is returned.
  2. This object has a property of getcrib, which reference a function whose name is called GET_PI_VALUE. This is the same case as code 1

Code 4:

.factory('cribs', function() {
  var data = 3.142;
  return {
    getcrib: function GET_PI_VALUE() {
      return data;
    }
  }
})
//console.log(cribs.getcrib()) outputs 3.142

Explanation:

  1. An object is returned.
  2. This object has a property of getcrib, which reference a function whose name is called GET_PI_VALUE. This is the same case as code 3.

Code 5

.factory('cribs', function() {
  var data = 3.142;
  return {
    getcrib: function GET_PI_VALUE() {
      return data;
    }
  }
})
//console.log(cribs.GET_PI_VALUE()) gives an error, complaining GET_PI_VALUE is not a function.

Explanation:

  1. An object is returned.
  2. This object has a property of getcrib
  3. GET_PI_VALUE is totally shadowed, hence error. The function of GET_PI_VALUE itself was NOT returned, only the reference(via getcribs) is returned.
Sign up to request clarification or add additional context in comments.

6 Comments

wow thank you very much! I get confused with returning a function without the bracket and with a bracket. Im really slow at this but this really helps! Thank you for a thorough explanation of a noob javascripter :D
blame those javascript inventors who come up with this confusing convention! :P
And javascript not having a proper class name, I thought he was returning an object instead of a function, I was also learning about prototype.constructor = ObjectNameOrfunctionName;and combined with what I saw . just confuses me. Oh a follow up question, if factory is being returned wouldnt the function inside be automatically be callable as well? or should he go something like this.function getcrib(){ .. }?
@Chopnut What a good follow up. No, the function insides are NOT callable. They are shadowed. You can only access what is returned. This is sort of a nice encapsulation in away. See my edit on Code 5
Wow awesome! I wouldnt have known that! Because any start up tutorial doesnt tell you that!Cause he didnt explain why he had to return a function, I was like thats weird.. Man youre a life saver! Thats it for now =) Super thanks! Done deal.
|

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.