What kind of code being written on AngularJS? -


i reading through angularjs source code of $http , like: wtf?

look @ line: https://github.com/angular/angular.js/blob/master/src/ng/http.js#l739

  promise.success = function(fn) {     promise.then(function(response) {       fn(response.data, response.status, response.headers, config);     });     return promise;   };    promise.error = function(fn) {     promise.then(null, function(response) {       fn(response.data, response.status, response.headers, config);     });     return promise;   };    return promise;    function transformresponse(response) {     // make copy since response must cacheable     var resp = extend({}, response, {       data: transformdata(response.data, response.headers, config.transformresponse)     });     return (issuccess(response.status))       ? resp       : $q.reject(resp);   } 

what's point of defining transformresponse function after return promise? never executed, right? inside file, find other place angularjs team keep adding code after return statement.

is there magic being thrown here or i'm on high?

long story short, if don't have time read calebboyd's link, can because

    function transformresponse(response) {...} 

is function declaration gets hoisted.

it work same reason work aswell:

    mysupersweetfunction(); // return 'hi there'      function mysupersweetfunction() { return 'hi there!' } 

however if did this:

    mysupersweetfunction(); // throw exception.      var mysupersweetfunction = function() {return 'hi there'} 

it wouldn't work, because although you're declaring function, making assignment variable. in case order write call matter.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -