jsContract 是一个实现了契约编程的 JavaScript 库。
示例:
function _internalMethod(a, b){ Contract.expectNumber(a); Contract.expectNumber(b); Contract.expectWhen(config.mode === "divide", b > 0, "Divisor cannot be 0"); Contract.expectWhen(config.mode === "multiply", a > 0 && b > 0, "The multiplicands cannot be 0"); Contract.guaranteesNumber(); Contract.guarantees(function(result){ return result > 0; }, "Result must be > 0"); if (config.mode == "divide") { return a / b; } // At this point config.mode must be "multiply" return a * b; }