Is it possible to inerhit the prototype chain from a function in a es6 class definition?
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);
Gives me the error:
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
If i use, extends
& super
, it works, but i have the properties of the duplexer stream on the Interface object, but i want to "hide" them, upper in the prototype chain.
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);
};
};
Not sure, if this is possible or i understand it correct, but when i inerhit some properties from a other class/object, they "stay" in its origin object scope, and when i want to access them the engine "looks" upwards till a propertie is found, if not, its undefined
.
My goal is to have my clean "Interace" object instance, with properties i pass to my consctrutor and still be able to use the node.js stream specific methods (read/write/etc.)
Thanks for any help.
EDIT: I found a workaround with a Proxy object:
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)
This checks if a property exists on the proxied object, if not, try to access the property on the hidden object.
It's a sure thing. You just need to amend the prototype of the subclass's prototype, and there you go.
Check out the code:
class Interface {}
Object.setPrototypeOf(Interface.prototype, Duplex.prototype)
const instance = new Interface
// you are able to call method from Duplex
instance.someMethodFromDuplex()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.