使用箭头函数设置变量
Set variable with Arrow Function
超级快速的问题,因为我已经习惯了箭头函数。我知道它们隐含地 return,并且我们可以隐含地 return 只要它是一个表达式。但是,例如,我可以减少以下内容吗
$scope.setEdit = () => { $scope.edit = true; }
到
$scope.setEdit = () => $scope.edit = true;
没有遇到任何不良行为?我不确定箭头函数在简单地分配变量时的行为方式。我只在通常有 return 语句的地方使用,从不使用简单的赋值。
这只是一个例子。谢谢!
赋值只是一个表达式,就像任何其他表达式一样:
const a = () => b = 1;
console.log(
c = 1,
a()
);
without encountering any undesired behavior?
这取决于调用者是否期望返回一些东西。如果没有,返回的结果将无处可去,那就没有区别了。
这与箭头函数关系不大,但与表达式求值有关。表达式的计算结果(可能有副作用)将是 returned 值。
这个箭头表达式函数:
() => a = b
与return是否相同:
() => {
return a = b;
}
这是一个有副作用的函数:a
得到一个值。赋值可以出现在表达式和 return 赋值中。所以相当于:
() => {
a = b;
return b;
}
我认为您对箭头函数的工作原理有误解。当您使用 shorthand 版本时,它们仅隐式 return。
这就是我的意思 - 这三个函数在功能上是相同的,它们都是 return 字符串 'hello'
function a() { return 'hello' }
const b = () => { return 'hello' }; //arrow Fn /w braces needs a return statement to return anything
const c = () => 'hello'; //shorthand arrow Fn w/o braces returns whatever the arrow points to
这是一个实际演示,使用了所有不同的函数样式
//regular function with no return value
var a = 'bar';
const myFunc1 = function() { a = 'foo'; }
console.log(myFunc1(), a);
//regular function with a return value
a = 'bar';
const myFunc2 = function() { return a = 'foo'; }
console.log(myFunc2(), a);
//arrow function with a return value
a = 'bar';
const myFunc3 = () => { return b = 'foo'; }
console.log(myFunc3(), a);
//arrow function with no return value
a = 'bar';
const myFunc4 = () => { b = 'foo'; }
console.log(myFunc4(), a);
//arrow function using shorthand, which has an implicit return value
a = 'bar';
const myFunc5 = () => b = 'foo';
console.log(myFunc5(), a);
超级快速的问题,因为我已经习惯了箭头函数。我知道它们隐含地 return,并且我们可以隐含地 return 只要它是一个表达式。但是,例如,我可以减少以下内容吗
$scope.setEdit = () => { $scope.edit = true; }
到
$scope.setEdit = () => $scope.edit = true;
没有遇到任何不良行为?我不确定箭头函数在简单地分配变量时的行为方式。我只在通常有 return 语句的地方使用,从不使用简单的赋值。
这只是一个例子。谢谢!
赋值只是一个表达式,就像任何其他表达式一样:
const a = () => b = 1;
console.log(
c = 1,
a()
);
without encountering any undesired behavior?
这取决于调用者是否期望返回一些东西。如果没有,返回的结果将无处可去,那就没有区别了。
这与箭头函数关系不大,但与表达式求值有关。表达式的计算结果(可能有副作用)将是 returned 值。
这个箭头表达式函数:
() => a = b
与return是否相同:
() => {
return a = b;
}
这是一个有副作用的函数:a
得到一个值。赋值可以出现在表达式和 return 赋值中。所以相当于:
() => {
a = b;
return b;
}
我认为您对箭头函数的工作原理有误解。当您使用 shorthand 版本时,它们仅隐式 return。
这就是我的意思 - 这三个函数在功能上是相同的,它们都是 return 字符串 'hello'
function a() { return 'hello' }
const b = () => { return 'hello' }; //arrow Fn /w braces needs a return statement to return anything
const c = () => 'hello'; //shorthand arrow Fn w/o braces returns whatever the arrow points to
这是一个实际演示,使用了所有不同的函数样式
//regular function with no return value
var a = 'bar';
const myFunc1 = function() { a = 'foo'; }
console.log(myFunc1(), a);
//regular function with a return value
a = 'bar';
const myFunc2 = function() { return a = 'foo'; }
console.log(myFunc2(), a);
//arrow function with a return value
a = 'bar';
const myFunc3 = () => { return b = 'foo'; }
console.log(myFunc3(), a);
//arrow function with no return value
a = 'bar';
const myFunc4 = () => { b = 'foo'; }
console.log(myFunc4(), a);
//arrow function using shorthand, which has an implicit return value
a = 'bar';
const myFunc5 = () => b = 'foo';
console.log(myFunc5(), a);