我正在尝试从节点模块中代理继承结构,并允许客户端实例化新的A类。目前,当尝试访问B类的父方法时,我得到a.parentMethod is not a function
handler.js ->
module.exports = {
get(target, key, receiver) {
return target.getAttribute(key)
},
set(target, key, value, receiver) {
return target.setAttribute(key, value)
}
}
A.js ->
const handler = require('handler')
class B {
constructor(data) {
this.data = data
}
parentMethod() {
... do stuff
}
}
class A extends B {
constructor(data){
super(data)
}
}
module.exports = function(data) {
return new Proxy(new A(data), handler)
}
////
const A = require('A')
var a = new A
a.parentMethod()
这个结构我哪里错了?我是Proxy的新手!
由于
编辑 -
进一步背景:
我正在尝试根据我定义的valueSchema保持属性集同步。当我设置Artwork.title时,我需要使用相同的值更新Artwork ['Artwork Title']。同样,当我检索Artwork.title时,我获得了Artwork ['Artwork Title']的价值。希望这有点帮助。我遇到了上述错误,所以我无法确定我写的内容确实有效!我正在尝试调试为什么首先找不到函数...
class Instance {
constructor(data) {
this._valueAttributes = {}
}
setAttribute(key, value) {
if (this._isValueAttribute(key)) {
return this._getSetValueAttribute(key, value)
}
throw Error('Cannot set invalid property '+key+' on instance.')
}
getAttribute(key) {
if (this._isValueAttribute(key)) {
return this._getSetValueAttribute(key)
}
}
_getSetValueAttribute(key, value) {
let schemaKey = this._getSchemaKey(key)
if (_.isFunction(schemaKey)) {
return alias(data)
}
if (value === undefined) {
return this._valueAttributes[schemaKey]
}
return this._valueAttributes[schemaKey] = value
}
_isValueAttribute(key) {
return _.keys(this._valueSchema).indexOf(key) === -1
}
}
class Artwork extends Instance {
constructor() {
this._valueSchema = {
medium: 'Artwork Medium',
title: 'Artwork Title'
}
}
}
///
a = new Artwork
a.title = 'thing'
a['Artwork Medium'] = 'medium';
我需要
a.title == a['Artwork Title']
a['Artwork Medium'] == a.medium
我很可能已经把它搞砸了。我假设我可以从父级访问子级上的__valueSchema。这不可能吗?