【发布时间】:2014-01-15 02:17:20
【问题描述】:
对于只执行一次的函数是否有官方术语或词?我能想到的最简单的例子是像这样的闭包中的 javascript 函数。 (取自here)
function createChance() {
var chanceLeft = true;
return function() {
if (chanceLeft) {
console.log("This was the only chance you had.");
chanceLeft = false;
} else {
console.log('Sorry: you already used your chance');
}
};
}
var chance = createChance();
chance();
// This was the only chance you had.
chance();
// Sorry: you already used your chance
【问题讨论】: