我正在查看以下代码,来自 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/