promise then (...args) reduce Promise.resolve()
let applyAsync = (acc,val) => acc.then(val);
let composeAsync = (...funcs) => x => funcs.reduce(applyAsync, Promise.resolve(x));
for (let f of [func1, func2]) {
await f();
}
来自:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
mozilla 上的一个关于闭包的例子
<p id="help">Helpful notes will appear here</p>
<p>E-mail: <input type="text" id="email" name="email"></p>
<p>Name: <input type="text" id="name" name="name"></p>
<p>Age: <input type="text" id="age" name="age"></p>
function showHelp(help) {
document.getElementById('help').innerHTML = help;
}
function setupHelp() {
var helpText = [
{'id': 'email', 'help': 'Your e-mail address'},
{'id': 'name', 'help': 'Your full name'},
{'id': 'age', 'help': 'Your age (you must be over 16)'}
];
for (var i = 0; i < helpText.length; i++) {
var item = helpText[i];
document.getElementById(item.id).onfocus = function() {
showHelp(item.help);
}
}
}
setupHelp();
运行结果跟你想象的一样吗?
来自:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
这是一个用js执行多遍的回答,使用了ES6语法
https://stackoverflow.com/questions/30452263/is-there-a-mechanism-to-loop-x-times-in-es6-ecmascript-6-without-mutable-varia
const times = x => f => {
if (x > 0) {
f()
times (x - 1) (f)
}
}
// use it
times (3) (() => console.log('hi'))
// or define intermediate functions for reuse
let twice = times (2)
// twice the power !
twice (() => console.log('double vision'))
其实,我认为这种写法除了ES6语法的唬人,并没有什么好处,
因为,需要的栈空间与循环次数成正比。 从这方面讲,不如使用 for(var i=0;i<99;++i){ }
但是,可以作为一个学习ES6语法的好例子,看下图:
== 与 === 的区别
https://www.codecademy.com/en/forum_questions/558ea4f5e39efed371000508