可能有时间延迟(例如,在浏览器环境setTimeout或类似环境中)和标志。不可取,但可能。
没有它,不,你不能将构造函数的动作建立在尚未发生的事情上。相反,您必须将某些内容传递给构造函数以让它知道发生了什么。
浏览器示例(同样,我不推荐这个):
function OutputNumber(number) {
var handle = 0;
this.outputThisInstead = function (otherNumber) {
if (handle) {
clearTimeout(handle);
handle = 0;
}
console.log(otherNumber);
}
handle = setTimeout(function() {
console.log(number);
}, 0);
}
根据您对问题的评论:
这是我正在试验的一系列链接对象/函数的结尾。例如:
Assert.that(1).is.not(2).because('output this message if fails');
这里 not(2) 返回一个对象,因为可以选择调用该对象。对象的行为将取决于被调用。
与其让链中较早函数的行为依赖于链中较晚的函数,我可能会.end()在某些内容的末尾添加一个:
Assert.that(1).is.not(2).because('output this message if fails').end();
end将输出先前函数存储的任何消息/消息。不需要黑魔法。显然,这会受到人们可能无法打开的事实的影响,但是如果您希望输出基于可选的后续函数调用而更改,则.end()您需要某种触发器来执行输出是可以的。