您的位置:首页 > 开源项目 > 常用JavaScript包 > jsContract

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;
}
更多

开发语言: javascript

授权协议:未知

相关标签