且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.then(functionReference) 和 .then(function(value){return functionReference(value)}) 之间有区别吗?

更新时间:2023-12-04 11:09:34

可以创建一个case,在不传递参数的情况下有差异,但这是一个延伸,一般你应该传递f 而不是 function(x) { return f(x);}x =>f(x) 因为它更干净.

It is possible to create a case where there is a difference when no argument is passed, but it is a stretch and generally you should pass f and not function(x) { return f(x); } or x => f(x) because it is cleaner.

这是一个导致差异的示例,其基本原理是采用参数的函数可能会对这些参数产生副作用:

Here is an example causing a difference, the rationale is that functions that takes parameters can cause side effects with those parameters:

function f() {
   if(arguments.length === 0) console.log("win");
   else console.log("Hello World");
}
const delay = ms => new Promise(r => setTimeout(r, ms)); // just a delay
delay(500).then(f); // logs "Hello World";
delay(500).then(() => f()) // logs "win"