【发布时间】:2021-04-28 08:55:24
【问题描述】:
是否可以从 es6 类定义中的函数 inerhit 原型链?
const { PassThrough } = require("stream");
const duplexer = require("duplexer3");
class Interface {
constructor(obj, options) {
options = Object.assign({
decodeStrings: false,
autoClose: false,
objectMode: true
}, options);
let reader = new PassThrough(options);
let writer = new PassThrough(options);
// use duplex stream as prototype
this = duplexer(options, reader, writer);
Object.assign(this, obj);
};
};
const iface = new Interface({
_id: "60891a02256c0c7931395c48",
settings: {
port: 8080,
host: "127.0.0.1"
}
});
this = duplexer(options, reader, writer);
给我错误:
this = duplexer(options, reader, writer);
^^^^
SyntaxError: Invalid left-hand side in assignment
at Object.compileFunction (vm.js:344:18)
at wrapSafe (internal/modules/cjs/loader.js:1048:15)
at Module._compile (internal/modules/cjs/loader.js:1082:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1138:10)
at Module.load (internal/modules/cjs/loader.js:982:32)
at Function.Module._load (internal/modules/cjs/loader.js:875:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
如果我使用extends 和super,它可以工作,但我在接口对象上有双工器流的属性,但我想“隐藏”它们,在原型链的上层。
class Interface extends duplexer {
constructor(obj, options) {
options = Object.assign({
decodeStrings: false,
autoClose: false,
objectMode: true
}, options);
let reader = new PassThrough(options);
let writer = new PassThrough(options);
super(options, reader, writer)
Object.assign(this, obj);
};
};
不确定,如果这是可能的或者我理解正确,但是当我从其他类/对象中获取某些属性时,它们“停留”在其原始对象范围内,当我想访问它们时,引擎“看起来" 向上直到找到一个属性,如果没有,它的undefined。
我的目标是拥有干净的“交互”对象实例,将属性传递给我的构造函数,并且仍然能够使用 node.js 流特定方法(读/写/等)
感谢您的帮助。
编辑:我找到了使用代理对象的解决方法:
class Root {
constructor() {
this.root = true;
}
rootMethod() {
console.log("i am root");
}
}
class Parent extends Root {
constructor() {
super();
this.parent = true;
}
parentMethod() {
console.log("i am parent");
}
}
class Child extends Parent {
constructor() {
super();
this.child = true;
}
childMethod() {
console.log("i am child")
}
}
class Interface {
constructor() {
this.settings = {
host: "0.0.0.0",
port: 8030
}
}
}
const root = new Root();
const parent = new Parent();
const child = new Child();
const iface = new Interface();
const proxy = new Proxy(child, {
get: function (target, prop) {
if (target[prop]) {
return target[prop];
} else {
return iface[prop];
}
}
});
//console.log(root, root.rootMethod);
//console.log(parent, parent.parentMethod);
//console.log(child, child.childMethod);
console.log(proxy.settings, proxy.rootMethod)
这会检查代理对象上是否存在属性,如果不存在,则尝试访问隐藏对象上的属性。
【问题讨论】:
-
更喜欢组合而不是继承 - 只是有一个
.#duplex成员并将流接口委托给它? (或者更好 - 改用异步迭代器:D) -
@BenjaminGruenbaum 感谢您的回复。你能给我一个“组合”的描述/链接/示例吗?第一次听说...这看起来很简单,但我想不通,这让我发疯了
-
我理解这个概念,但我无法将代码放在一起解决我的问题。我所做的只是“接口”范围被父/双工器方法/属性污染。我不知道为什么...pastebin.com/raw/ihGvi1aX是我能做的最接近的,但这里是类名“流”显示在
console.log,这不太好......我不明白,在普通的 js 对象中它也可以工作...... -
@BenjaminGruenbaum 您如何看待我的解决方法,使用代理对象?
标签: javascript class inheritance stream