【发布时间】:2018-02-26 09:14:31
【问题描述】:
目前正在尝试对 Array / Object 进行自定义实现(我想最终会非常相似)并且偶然发现了一个让我发疯的问题。
如你所见,b 只是一个 instanceOf 数组,尽管它是从自定义类 CachedArray 创建的,因此我的自定义函数 testPush 没有定义,我无法为了一切找到问题所在。
使用 Nodejs 6
function _setKey(target, key, value) {
console.log('Setting value', key, 'to', value);
target[key] = value;
return true;
}
class ExtendableProxy {
constructor(a, b) {
return new Proxy(a, b);
}
}
class CachedArray extends ExtendableProxy {
constructor(redis, options) {
let RawArray = [];
super(RawArray, {
set: _setKey
});
this._rawArray = RawArray;
this.redis = redis;
this.options = options;
}
testPush() {
this.push('Its me');
}
}
var b = new CachedArray();
console.log('b instanceof CachedArray', b instanceof CachedArray); //false
console.log('b instanceof ExtendableProxy', b instanceof ExtendableProxy); //false
console.log('b instanceof Proxy', b instanceof Proxy); //false
console.log('b instanceof Array', b instanceof Array); //true
b.push('Hello.'); //Works just fine, _setKey is called and executed correctly
b.testPush(); //TypeError: b.testPush is not a function
【问题讨论】:
-
这不是应该如何创建代理的。代理是对象的包装器,但不是类型。您正在尝试定义一个扩展您的基础对象的类型(在您的情况下为
Array),但代理无法通过扩展原型来工作,因此没有继承链。 -
我明白了,这可以解释为什么我一开始就不能扩展代理。有没有替代我想要实现的目标?本质上,我需要一个带有我的一些额外功能的数组,但是它有一个连接到它的代理,这样我就可以进一步处理发生在我的类的实例上的任何写入(所以,数组)
-
仅供参考,这是我尝试使用代理功能扩展数组的尝试,您可以查看here。
标签: javascript arrays node.js ecmascript-6