任务:我需要在Typescript中构建一个类,以在其自己的Constructor中调用其某些方法。
问题:以下示例代码表示的实际代码将成功编译,但是在Javascript控制台中进行测试后,它不会成功编译。
样品:
export class volumeEQ
{
constructor(ctx:any)
{
this.ctx = ctx; // Audio context saved into member variable of class
this.setupAudioNodes(); // Sets up nodes made out of audio
}
setupAudioNodes()
{
this.sourceNode.connect(this.ctx.destination); // Connect to destination
}
}
技术: Typescript编译器没有问题,this.setupAudioNodes()
但是曾经在浏览器的Javascript控制台中被称为Javascript ,但我收到了错误消息Uncaught TypeError: undefined is not a function
。实际上,这是Javascriptthis.
语法的一个问题,并且很容易与它混淆。但是因为我正在使用Typescript开发,所以我想要一个更多的Typescript样式解决方案。
问题:如何从Typescript的构造方法中调用类的方法?
这是从构造函数调用方法的方法:
class Thing {
constructor(name: string) {
this.greet(name);
}
greet(whatToGreet: string) {
console.log('Hello, ' + whatToGreet + '!')
}
}
var x = new Thing('world'); // Prints "Hello, world!"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句