Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 2使用箭头函数订阅与使用bind命名函数变量订阅_Javascript_Angular_Typescript - Fatal编程技术网

Javascript Angular 2使用箭头函数订阅与使用bind命名函数变量订阅

Javascript Angular 2使用箭头函数订阅与使用bind命名函数变量订阅,javascript,angular,typescript,Javascript,Angular,Typescript,订阅Http可观察对象时,大多数教程使用箭头函数: this.myService.getItems.subscribe( (data) => // do something, (errors) => //handle errors); 为什么不使用函数变量来生成更清晰的代码: this.myService.getItems.subscribe(this.extractData.bind(this), this.handleErrors.bind(this)); 这取决于你的风格。

订阅Http可观察对象时,大多数教程使用箭头函数:

this.myService.getItems.subscribe(
(data) => // do something,
(errors) => //handle errors);
为什么不使用函数变量来生成更清晰的代码:

this.myService.getItems.subscribe(this.extractData.bind(this), this.handleErrors.bind(this));

这取决于你的风格。第一次使用data=>{do something}看起来有点混乱,但使用得越多,就越舒服。您会喜欢它并且不想使用此.extractData.bindthis

this.function.bindthis是否更干净?我不同意。是的,当涉及到+10行的嵌入式箭头函数时