ES5 class inherits from ES6 class (with proxy)
by Arnaud Buchholz
JavaScript
window.onerror = e => alert(e)
class A {
constructor () {
this._a = "A"
}
}
class B extends A {
constructor (param) {
super()
this._b = "B"
this._param = param
}
}
alert(B.toString())
function constructorOfC (param2) {
this.$super("test")
this._c = "C"
this._param2 = param2
}
const C = (function () {
function C () {
let instance
const $super = function () {
instance = Reflect.construct(B, arguments, C)
}
const proxy = new Proxy({}, {
get: function (obj, property) {
if (property === '$super') {
return $super
}
return instance[property]
},
set: function (obj, property, value) {
instance[property] = value
}
})
constructorOfC.apply(proxy, arguments)
return instance
}
C.prototype = Object.create(B.prototype)
return C
}())
var c = new C("test2");
alert([
"OK",
"_a: " + c._a,
"is A: " + (c instanceof A),
"_b: " + c._b,
"is B: " + (c instanceof B),
"_c: " + c._c,
"is C: " + (c instanceof C),
"_param: " + c._param,
"_param2: " + c._param2
].join("\n\t"))