Javascript 财产的类别
我想做以下工作:Javascript 财产的类别,javascript,typescript,class,Javascript,Typescript,Class,我想做以下工作: someProperty = myFunction().someFunction().someOtherFunction() 然而,最后一部分在不同的子类中被多次调用,所以我这样做: 类别: class Wheelies { property0 = someFunction().someOtherFunction() } 我想在一个子类中调用getWheels,就像这样,但是这不起作用: class Car extends Wheelies { property1 = m
someProperty = myFunction().someFunction().someOtherFunction()
然而,最后一部分在不同的子类中被多次调用,所以我这样做:
类别:
class Wheelies {
property0 = someFunction().someOtherFunction()
}
我想在一个子类中调用getWheels,就像这样,但是这不起作用:
class Car extends Wheelies {
property1 = myFunction().this.getwheels
}
你们能帮帮我吗,我的问题是我不知道为什么它不起作用,我也不知道该怎么做。这里有一个很好的解释 但基本上,您需要返回(this),以便可以链接函数调用
class Wheelies {
constructor(property1) {
this.property1 = property1;
}
someFunction() {
this.property1 = this.property1 + 'x'
return this
}
someOtherFunction() {
this.property1 = this.property1 + 'y'
return this
}
}
class Car extends Wheelies {
// this inherits from Wheelies
}
const myCar = new Car("MyCar")
console.log(myCar.property1) //"MyCar"
console.log(myCar.someFunction().property1) // "MyCarx"
console.log(myCar.someFunction().someOtherFunction().property1) // "MyCarxxy"
您还可以更改函数调用的顺序以产生不同的结果。这里有一个很好的解释,但基本上您需要返回(this),以便可以链接函数调用。您可能正在寻找多态的
this
,但您的操作还不完整。。你能提供一个简单的例子来重现你想做的事情吗?你的问题有点不清楚,你到底想用你的代码实现什么?访问方式如下->property1=this.property0.this.getwheelsproperty1=myFunction()[this.property0]
或property1=myFunction()[this.getwheels]
如果您有property0
的getter。这是你想要的吗?