问题
I'm trying to Proxy a ES6 constructor (mostly trying to emulate Python's descriptors for fun and learning):
class _Record {
constructor(data) {
this._data = data;
}
}
const constrProxyHandlers = {
construct: function(target, args, cstr) {
let instProxyHandler = {
get: (target, prop) => {
if(prop in target._data) { return target._data[prop] }
return target[prop]
},
set: (target, prop, val) => {
if(prop in target._data) {
target._data[prop] = val;
} else {
target[prop] = val;
}
return true
}
}
let obj = Object.create(cstr.prototype)
cstr.apply(obj, args)
return new Proxy(obj, instProxyHandler)
}
}
const Record = new Proxy(_Record, constrProxyHandlers)
let rec = new Record({name: 'Vic', age: 25})
console.log([rec.name, rec.age])
rec.name = 'Viktor'
console.log(rec)
If you run this snippet, you'll get this error:
cstr.apply(obj, args)
TypeError: Class constructor _Record cannot be invoked without 'new'
If I replace cstr.apply
with new cstr
, the constructor quickly exhausts stack (apparently going into an infinite recursion).
This works if I replace the _Record
class with a function (for example, this would work transpiled through Babel). Can I make it work with native ES6?
Thank you.
P. S. I'm currently checking these snippets on Node 7.7.4, if it matters.
回答1:
The part
let obj = Object.create(cstr.prototype)
cstr.apply(obj, args)
does not work with ES6 classes. You need to use
let obj = Reflect.construct(target, args, cstr);
(and not Reflect.construct(cstr, args)
or new cstr(...args)
that you were trying to do, which indeed recurses infinitely - IIRC target
refers to _Record
while cstr
refers to Record
or one of its subclasses).
来源:https://stackoverflow.com/questions/43104420/typeerror-when-trying-to-proxy-a-es6-class-constructor