【发布时间】:2019-07-11 20:07:42
【问题描述】:
我正在尝试找到一种方法将表达式(例如条件语句)转换为字符串,而无需处理表达式。
我尝试在以下“断言”函数中的“条件”参数上使用 .toString() 方法。
const config = {
usernme: 'username1',
password: 'password1'
}
function assert(condition, message) {
if (!condition) {
message = message || `Assertion failed: ${condition.toString()}`;
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}
assert('username' in config);
实际错误信息:Assertion failed: false
预期的错误消息:Assertion failed: 'username' in config
【问题讨论】:
标签: javascript string casting type-conversion expression