lambda - 在函数调用中作为参数传递时,粗箭头语法的含义是什么?

标签 lambda typescript angular

我正在查看以下代码,来自 here

import {Http} from 'angular2/http'
import {Injectable} from 'angular2/core'
@Injectable()
export class AddressBookService {
    http:Http;
    constructor(http:Http){
        console.log('Creating AddressBookService');
        this.http = http;
    }
    getEntries(){
        return this.http.get('./people.json').map(res => res.json());
    }
}

我无法理解 res => res.json() 的含义。我认为它一定是一个 lambda 函数,但我不明白它的意义所在。它不会返回和存储变量或执行任何看起来有用的操作。

如果有人能解释这是做什么的,那就太好了。

最佳答案

return this.http.get('./people.json').map(res => res.json());

ES6 syntax sugar在这种情况下,它与这个 ES5 兼容版本的功能相同:

return this.http.get('./people.json').map(function(res) {return  res.json();});

名称 map 及其在代码中的使用表明 this.http.get('./people.json') 将是某种集合,并且map 迭代每个元素,根据对每个元素执行函数的结果创建一个新集合。它的行为应该类似于 Array.map :

[1,2,3,4].map(x => x+1); // ==> [2,3,4,5]

关于lambda - 在函数调用中作为参数传递时,粗箭头语法的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36277470/

相关文章:

html - 当捕获到HTTP错误代码500时,显示一个不错的div

lambda - Scheme 中的 Y 组合器,使用惰性求值?

c++ - 在 lambda 表达式和模板 typedef 习语中提取对成员

typescript - TSLint:未使用的 var 关键字

javascript - 如何在 Typescript 中调用 $element

javascript - 将 JS 库 xml2js 与 Angular 2 结合使用

c# - 基于KeyValuePairs的过滤列表

c++ - 嵌套 Lambda 函数 - 性能影响

typescript - 一起使用 Typescript 可变元组类型和 Javascript Spread

json - 如何将 JSON 数据从 Node 发送到 Angular 4