Javascript 类 是否有一种标准的方式来相互引用?
Is there a standard way for Javascript classes to refer to each other?
我想知道 class 属性和方法是否有标准的方式在它们的声明中相互引用?例如:
class A {
methodA(){
b.methodB()
...
}
}
class B {
methodB(){...}
}
const a = new A();
const b = new B();
///will throw ReferenceError: Cannot access 'b' before initialization;
我知道你可以在所有 class/object 声明之后添加 methodA,但是在多次重复之后它变得非常不整洁,我觉得这违背了 classes 的目的。或者您可以通过其他方式延迟参考。有没有标准的方法来做到这一点?
嗯,你总是可以在你的 class 中继续实例化另一个 classes
,比如 A
并像这样访问它们的方法和属性。
class A {
methodA(){
const b = new B();
b.methodB()
}
}
class B {
methodB(){console.log("Here I am")}
}
const a = new A();
a.methodA();
或者,您可以使用 inheritance
。
class A {}
class B extends A{
constructor() { super(); //}
}
首先,Javascript没有"classes"。 class
关键字只是为了让它看起来很熟悉。它应该是 proto
而不是 class
。
最好用mixins
。此外,这在很大程度上取决于是否能够将对方法 B 的更改传播给 A 的所有后代,或者您是否希望这样做。
另见 "prose-like syntax"
无传播 - 复制
如果不需要传播,需要copy一大堆方法过来:
const A = {
name: 'I am A.'
}
const B = {
name: 'I am B.', // this will overwrite A when we #assign. so be careful to assign it back
methodB() {
console.log(`${this.name} - foo`)
}
}
const a = Object.create(A)
Object.assign(a, B)
a.name = A.name
// methodB is changed but it won't propagate.
B.methodB = function() {
console.log(`${this.name} - bar`)
}
a.methodB() // I am A. - foo
B.methodB() // I am B. - bar
随着传播。
如果您只需要一种或几种方法:
const A = {
name: 'I am A.'
}
const B = {
name: 'I am B.',
methodB() {
console.log(`${this.name} - foo`)
}
}
const a = Object.create(A)
a.methodA = function() {
B.methodB.call(this)
}
B.methodB = function() {
console.log(`${this.name} - bar`)
}
a.methodA() // I am A. - bar
B.methodB() // I am B. - bar
我想知道 class 属性和方法是否有标准的方式在它们的声明中相互引用?例如:
class A {
methodA(){
b.methodB()
...
}
}
class B {
methodB(){...}
}
const a = new A();
const b = new B();
///will throw ReferenceError: Cannot access 'b' before initialization;
我知道你可以在所有 class/object 声明之后添加 methodA,但是在多次重复之后它变得非常不整洁,我觉得这违背了 classes 的目的。或者您可以通过其他方式延迟参考。有没有标准的方法来做到这一点?
嗯,你总是可以在你的 class 中继续实例化另一个 classes
,比如 A
并像这样访问它们的方法和属性。
class A {
methodA(){
const b = new B();
b.methodB()
}
}
class B {
methodB(){console.log("Here I am")}
}
const a = new A();
a.methodA();
或者,您可以使用 inheritance
。
class A {}
class B extends A{
constructor() { super(); //}
}
首先,Javascript没有"classes"。 class
关键字只是为了让它看起来很熟悉。它应该是 proto
而不是 class
。
最好用mixins
。此外,这在很大程度上取决于是否能够将对方法 B 的更改传播给 A 的所有后代,或者您是否希望这样做。
另见 "prose-like syntax"
无传播 - 复制
如果不需要传播,需要copy一大堆方法过来:
const A = {
name: 'I am A.'
}
const B = {
name: 'I am B.', // this will overwrite A when we #assign. so be careful to assign it back
methodB() {
console.log(`${this.name} - foo`)
}
}
const a = Object.create(A)
Object.assign(a, B)
a.name = A.name
// methodB is changed but it won't propagate.
B.methodB = function() {
console.log(`${this.name} - bar`)
}
a.methodB() // I am A. - foo
B.methodB() // I am B. - bar
随着传播。
如果您只需要一种或几种方法:
const A = {
name: 'I am A.'
}
const B = {
name: 'I am B.',
methodB() {
console.log(`${this.name} - foo`)
}
}
const a = Object.create(A)
a.methodA = function() {
B.methodB.call(this)
}
B.methodB = function() {
console.log(`${this.name} - bar`)
}
a.methodA() // I am A. - bar
B.methodB() // I am B. - bar