我在 v8 中模拟 noSuchMethod 时遇到问题。我在命令行中正确激活了和谐代理(我没有使用 Proxy() 对象出现错误,并且我能够使用一些代理功能)但是我发现的所有示例都不适用于 noSuchMethod。
var NoSuchMethodTrap = Proxy.create({
// FIXME: don't know why I need to provide this method,
// JS complains if getPropertyDescriptor is left out, or returns undefined
// Apparently, this method is called twice: once for '_noSuchMethod_' and once for 'foo'
getPropertyDescriptor: function(n){ return {} },
get: function(rcvr, name) {
if (name === '__noSuchMethod__') {
throw new Error("receiver does not implement __noSuchMethod__ hook");
} else {
return function() {
var args = Array.prototype.slice.call(arguments);
return this.__noSuchMethod__(name, args);
}
}
}
});
function MyObject() {};
MyObject.prototype = Object.create(NoSuchMethodTrap);
MyObject.prototype.__noSuchMethod__ = function(methName, args) {
return 'Hello, '+methName;
};
我包含了我正在尝试的代码片段,但这有点毫无价值,因为无论我从哪里获得代码,情况都是一样的。我已经用谷歌搜索了一个星期了。那么问题来了:这难道根本没有在 v8 中实现吗?如果是这样,我错过了什么?