var cache = new Proxy(new Map(), {
apply: function(target, thisArg, argumentsList) {
console.log('hello world!');
}
});
cache.set('foo', 'bar');
至于我可以告诉这个应该结果hello world!
被记录到控制台和地图的foo
键未被设置。然而,当我运行它,它抛出:为什么我的代理包装Map函数会调用TypeError?
TypeError: Method Map.prototype.set called on incompatible receiver [object Object]
at Proxy.set (native)
at repl:1:7
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:340:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:537:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:189:7)
at REPLServer.Interface._onLine (readline.js:238:10)
我GOOGLE了又去了所有的MDN代理文档几次,我不能换我周围为什么这是行不通的头。
任何想法?我在Node.js 7.5.0上。
什么是你真正想实现什么?你能否创建一个'Map'的子类?例如,你可以在子类中重写'.set()'。 – jfriend00
@ jfriend00耶,重写'.set'是我最初的想法。我使用代理服务器的唯一原因是因为这个项目是教育性的,我想尝试新的闪亮:D – strugee
'应用'陷阱用于调用函数对象,并且不会在“Map”对象上工作(不可调用)。 – Bergi