nodejs 使用assert做参数验证

xiaoxiao2021-02-28  101

nodejs 自带assert 模块,一般用来做单元测试, 但也可以直接用在项目做,做参数验证,参数不符合要求直接抛出异常,比较方便

const assert = require("assert"); function sayHi(msg) { assert(msg.length > 10, "长度必须大于10"); console.log("hi " + msg); } sayHi("abc");
转载请注明原文地址: https://www.6miu.com/read-60502.html

最新回复(0)