0

The title doesnt really explain what i am trying to ask here. Well here is an example:

.factory('Story', function($http) {
   var storyFactory = {};
   Factory.getStory = function() {
      return $http.get('/api');
   }
})


.controller('StoryController', function(story) {

var vm = this;

Story.allStory()
    .sucess(function(mydata) {

    })
})

So how allStory() returns data into mydata?

Photonic
  • 1,316
  • 19
  • 31

2 Answers2

1

Are you asking about the way javascript replaces the written code with the logical object at the time?

eg.

console.log(new Array({"getWalkDetails":function(){return {"MaxSpeed":15, "DistanceWalked": 123}} },
                       "walking on sunshine", 
                       "oh oh" ).shift().getWalkDetails().MaxSpeed);
//outputs "15" to the console

This can be rewritten as

var arr = new Array();
var func = function(){
        var details = new Object();
        details.MaxSpeed =15;
        details.DistanceWalked = 124;
        return details;
}
var obj = {"getWalkDetails" : func};
arr.push(obj);
arr.push("walking on sunshine");
arr.push("oh oh");

var firstItem = arr.shift();
//the array function 'shift()' is used to remove the first item in the array and return it to the variable
var walkingDetails = firstItem.getWalkingDetails()//same as func() or obj.getWalkingDetails()
console.log(walkingDetails.MaxSpeed);//15

As you can see we stored most of the the interpreted outputs as variables to be used seperately.

EDIT: If you are asking how to pass objects by reference in javascript to allow the mydata variable to receive any changes done to it in the function it is passed to. then this question might be helpful to you: javascript pass object as reference

EDIT: edited the code above a bit more

Community
  • 1
  • 1
Sarfaraaz
  • 488
  • 6
  • 17
  • Thanks that explains it. Javascript is much harder to understand the syntax than any other language – Photonic May 26 '15 at 02:58
0

I'm not entirely sure either what you mean (especially because I never worked with Angular), but I have a feeling you're puzzled by this little trick:

//+ fn -> [a]
var getParameterNames = (function () {
    var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
        ARGUMENT_NAMES = /([^\s,]+)/g;

    return function (fn) {
        var fnStr = fn.toString().replace(STRIP_COMMENTS, ''),
            names = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);

        return names || [];
    };
})();

function add (x, y) {
    return x + y;
}

console.log(getParameterNames(add));    // => ['x', 'y']

Edit: And here's a jsfiddle.