简体   繁体   中英

What does “function() { get: function(…) { … } }” in JavaScript mean?

I found the following code snippet here :

App.factory('myHttp',['$http',function($http) {
  return function() {
    get: function(url, success, fail) {
      $http.get(url).success(function(response) {
        return response.data;
      }).error(fail);
    }
  };
}]);

I wonder what does this syntax mean:

function() {
  get: function(...) { ... }
}

It looks like it's a typo. It's definitely a syntax error. It resembles the ES5 getter notation , but even so, you can only use that with a property name, like get response() { ... } .

Perhaps what the author intended was:

App.factory('myHttp',['$http',function($http) {
  return {
    get: function(url, success, fail) {
      $http.get(url).success(function(response) {
        return response.data;
      }).error(fail);
    }
  };
}]);

which is not the ES5 getter notation, but a simple object with one property called get , referring to HTTP GET (as opposed to POST).

I'm reasonably sure a JavaScript parser would interpret get there as a label . It's useless, because labels are only useful for loop control, and there is no loop here. Still, it's mostly legal syntax. That said, Firefox's interpreter will complain about a dead-code anonymous function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM