我需要能够从子类访问父(而不是超级)类。
在我的情况下,数据结构正好变为3级深度:
A类包含一个x * Class B的数组,其中包含一个y * C类的数组。
C类实例需要能够访问A类的属性。
我的想法是传递回调,这将返回所需的值。 我可以不传递来自A类的静态值,因为它们可以在运行时更改。
这是处理我的实例之间数据流的正确方法,还是可以更优雅的方式实现?
我为这个案例设置了一个小提琴(为了简单起见,只有2个级别):https://jsfiddle.net/2chngzqg/
class A {
constructor() {
this.foo = "bar";
}
shareProps = () => {
return this.foo;
}
createSubClass() {
this.sub = new B(this.shareProps);
}
}
class B {
constructor(callback) {
this.logShared = callback;
}
}
const a = new A();
a.createSubClass();
console.log(a.sub.logShared())
a.foo = 'baz';
console.log(a.sub.logShared())
答案 0 :(得分:1)
那怎么样?..如果已经提到的Cart
类型包含Product
类型的列表,并且每个product
确实列出了其Option
类型,并且看到了从示例中,每种类型都控制着如何创建和/或添加其他依赖类型到它的列表,然后......只是将托管类型的这些信息提供给托管类型的构造函数/工厂函数因此,每option
个product
插槽/属性,每个product
都有一个cart
插槽/属性。
然后changed code of the OP's provided fiddle可能看起来像......
class Foo {
constructor(host) {
this.foo = "foo";
this.host = host;
}
createMember(Member) {
return (this.member = new Member(this));
}
}
class Bar {
constructor(host) {
this.bar = "bar";
this.host = host;
}
createMember(Member) {
return (this.member = new Member(this));
}
}
class Baz {
constructor(host) {
this.baz = "baz";
this.host = host;
}
createMember(Member) {
return (this.member = new Member(this));
}
}
var foo = (new Foo);
console.log('foo : ', foo);
console.log('foo.foo : ', foo.foo);
console.log('foo.member : ', foo.member);
console.log('foo.host : ', foo.host);
var bar = foo.createMember(Bar);
console.log('bar : ', bar);
console.log('bar.bar : ', bar.bar);
console.log('bar.member : ', bar.member);
console.log('bar.host : ', bar.host);
console.log('bar.host.host : ', bar.host.host);
var baz = bar.createMember(Baz);
console.log('baz : ', baz);
console.log('baz.baz : ', baz.baz);
console.log('baz.member : ', baz.member);
console.log('baz.host : ', baz.host);
console.log('baz.host.host : ', baz.host.host);
console.log('baz.host.host.host : ', baz.host.host.host);

.as-console-wrapper { max-height: 100%!important; top: 0; }

答案 1 :(得分:-1)
虽然不是es6 - 你可以使用功能继承来实现这一点。
使用对象that
来保存类属性。在此示例中,B
被实例化到A
的范围内。
const A = () => {
let that = {}
// Public variable
that.foo = 'bar';
// Public function
that.shareProps = () => (that.foo);
// Subclass functions attached to this classes scope
that.createSubClass = () => {
that = B(that);
}
return that;
}
const B = (that) => {
that.logShared = () => (that.shareProps());
return that;
}
const a = A();
a.createSubClass();
console.log(a.logShared())
a.foo = 'baz';
console.log(a.logShared())
如果需要,'子类'可以附加到函数内的其他公共变量上。
当您从'child'类继承'parent'类时,这会更灵活,因为您始终可以访问添加到that
的任何函数和变量。
const A = () => {
let that = {}
that.foo = 'bar';
that.shareProps = () => (that.foo);
return that;
}
const B = () => {
// A is inherited
let that = A();
that.logShared = () => (that.shareProps());
return that;
}
const C = () => {
// B is inherited
let that = B();
return that;
}
const c = C();
// Calls the function in B() => that.logShared()
console.log(c.logShared())
// Changes the value in A() => that.foo
c.foo = 'baz';
console.log(c.logShared())
有关功能继承的更多示例,请参阅here。