简体   繁体   English

带有ES6赋值的箭头函数语法

[英]Arrow function syntax with assignment ES6

Quick question, I'm new with es6 syntax and typscript and I just found this code in my current ng2 project: 快速问题,我是es6语法和打字稿的新手,我刚刚在当前的ng2项目中找到了以下代码:

this.contextActions.getCurrentContextAction().subscribe((link) => this.currentAction = link);

Just to be clear if I understand it correctly. 请弄清楚我是否正确理解。

The function subscribe takes as argument a function which takes a parameter (link) and it assigns the input parameter to the current action, and it returns the result of this assigment? 函数subscribe接受一个带有参数(链接)的函数,并将输入参数分配给当前操作,并返回该赋值的结果?

Is it correct my understanding? 我的理解正确吗? What is actually what this arrow function returns? 这个箭头函数实际上返回什么? The result of the assigment? 交战的结果? An assigment does not return anything right? 分配不返回任何内容,对不对?

Please clrify this to me. 请把这个弄清楚。

Thanks 谢谢

Is it correct my understanding? 我的理解正确吗? What is actually what this arrow function returns? 这个箭头函数实际上返回什么? The result of the assigment? 交战的结果?

Yes. 是。 It may not be significant depending on what the code calling it does with the return value. 它可能并不重要,具体取决于调用它的代码对返回值的处理方式。

An assigment does not return anything right? 分配不返回任何内容,对不对?

It evaluates as the assigned value. 评估为分配的值。

 var bar, foo; foo = (bar = 1); console.log("Foo is " + foo); 

An assignment does not return anything right? 作业不会返回任何内容吗?

Wrong, the value of an assignment is the value that is assigned: 错误的是,分配的值就是分配的值:

 var a; console.log(a = 5); 

This is why you can do things like this: 这就是为什么您可以执行以下操作的原因:

 var a, b, c; console.log(a = b = c = 5); // all have the value 5 

So the arrow function returns its parameter, link . 因此,箭头函数返回其参数link I don't know if the subscribe function does something with the returned value. 我不知道subscribe功能是否对返回的值进行了处理。 Presumably not. 大概不是。

The rest of your understanding is correct. 您剩下的理解是正确的。

This is effectively the same as: 这实际上与以下内容相同:

var _this = this;
this.contextActions
  .getCurrentContextAction()
  .subscribe(function(link) { 
    return _this.currentAction = link
  });

Subscribe accepts a function that takes a single parameter (link) that will be internally called by subscribe at a certain stage. 订阅接受一个带有单个参数(链接)的函数,该参数将在某个阶段由订阅内部调用。

In this context, link is an internally generated variable that is given to the function you are passing in as input. 在这种情况下,链接是内部生成的变量,该变量被提供给您作为输入传递的函数。 You could alternatively do the following: 您也可以执行以下操作:

function myFunction(link) { 
  return _this.currentAction = link
}

var _this = this;
this.contextActions
  .getCurrentContextAction()
  .subscribe(myFunction);

The arrow function will assign the value given to your callback to this.currentAction and then return the value in a single statement. 箭头函数会将分配给您的回调的值分配this.currentAction ,然后在单个语句中返回该值

Eg: 例如:

var a, b, c;

console.log(a = 5); // Logs out 5

function test() {
  return b = 6;
}

c = test();
console.log(b); // Logs out 6
console.log(c); // Logs out 6

A good way to test this is to open up a browser and see what it does. 测试它的一个好方法是打开浏览器并查看它的作用。 Let's see what happens in Chrome Dev tools when console log an assignment. 让我们看看在控制台记录分配时Chrome开发工具中会发生什么。

var someNum = 5;

console.log(someNum = 20);

20 20

If you did not use => it would look like this 如果您不使用=> ,它将看起来像这样

var _that = this;
.subscribe(function(link){
   return _that.currentAction = link;
}

It returns the assigned value. 它返回分配的值。

See the docs . 请参阅文档

(param1, param2, …, paramN) => { statements } (param1,param2,…,paramN)=> {语句}
(param1, param2, …, paramN) => expression (param1,param2,…,paramN)=>表达式
// equivalent to: (param1, param2, …, paramN) => { return expression; //等于:(param1,param2,…,paramN)=> {返回表达式; } }

// Parentheses are optional when there's only one parameter name: //只有一个参数名称时,括号是可选的:
(singleParam) => { statements } (singleParam)=> {语句}
singleParam => { statements } singleParam => {语句}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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