【发布时间】:2017-09-28 21:55:32
【问题描述】:
使用以下代码:
const assrt = function () {
try {
return chaiAssert.apply(null, arguments);
}
catch (e) {
return handleError(e);
}
};
v.assert = new Proxy(assrt, {
get: function (target, prop) {
if(typeof prop === 'symbol'){
// I don't know what to do with symbols, so return
return Reflect.get(...arguments);
}
// but here! we still get properties that don't exist
if(!chaiAssert[prop]){
return handleError(
new Error(`The assertion library used does not have '${prop}' property or method.`)
);
}
return function () {
try {
return chaiAssert[prop].apply(null, arguments);
}
catch (e) {
return handleError(e);
}
}
}
});
我使用此代码得到的错误是:
TypeError:无法将符号值转换为字符串
这发生在行:
new Error(`The assertion library used does not have '${prop}' property or method.`));
我以前使用过代理,但从未见过将符号传递给代理的 get 方法。有谁知道如何规避这个问题?
为什么要将符号传递给代理获取函数,我该如何正确处理?
【问题讨论】:
-
被发送到 get 函数的 prop 是
Symbol(util.inspect.custom),并且认为还有其他的被发送。 -
我认为,您必须调试代码。
if(typeof prop === 'symbol'){似乎不需要过滤掉符号。
标签: javascript node.js es6-proxy