0

I am a javascript noob so i dont understand why in the code below the author uses Avengers.cast instead of just Avengers?

var myApp = angular.module('myApp', []);
    myApp.factory('Avengers', function() {
        var Avengers = {};
        Avengers.cast = [{
            name: "Robert Downey Jr.",
            character: "Tony Stark / Iron Man"
        }];
        return Avengers;
    })


function AvengersCtrl($scope, Avengers) {
    $scope.avengers = Avengers;
}
3
  • 2
    He is setting a property cast on the object Avengers. It can be accessed using the same notation. Commented Feb 25, 2014 at 11:55
  • @lethal-guitar: Well there's also the question of why set it on the property instead of the variable... Commented Feb 25, 2014 at 11:56
  • 4
    apparently design decision - the Avengers object will probably have more properties in the future than just the cast Commented Feb 25, 2014 at 12:00

3 Answers 3

1
    var Avengers = {};

This means: create a new object and refer to it by the variable Avengers

    Avengers.cast = [{
        name: "Robert Downey Jr.",
        character: "Tony Stark / Iron Man"
    }];

This means: the object just created now has a property called cast. This is an Array and contains exactly one object (with two properties with String values).

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

1 Comment

So why declare var Avengers = {}; and Avengers.cast when the code seems to work just the same when i delete the var Avengers = {}; line and change Avengers.cast to Avengers? Thank you so much for your answer!
0

In your code, cast property of Avengers is set to specific object. Let say, initially Avengers has no value. In factory section, Avengers became

Avengers.cast = [{
    name: "Robert Downey Jr.",
    character: "Tony Stark / Iron Man"
}];

You can use this somewhere in your code like console.log(Avengers.cast). Following example for the function;

myApp.factory('Avengers', function() {
    return {
        helloWorld: function() {
            return "Hello, World!"
        }
    };
});

You can use Avengers.helloWorld() somewhere in your code.

As summary, you can create objects with angular.js factory and set property for that object also create functions in that object. Refer here for more details

Comments

0

To answer your specific question: there is more than one way to pass information around.

The author could have made the Avengers service just the array instead of an object with the array.

So the choice was somewhat arbitrary, as you seem to be picking up on.

That said if the author is planning on extending the Avengers service with more properties, then that is why an object with the array was chosen.

Although if you're just going to do that you should check out constant - maybe it would be more appropriate than factory.

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.