Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  90] [ 6]  / answers: 1 / hits: 17474  / 10 Years ago, wed, may 21, 2014, 12:00:00

Task: I need to build a class in Typescript that calls some of it's own methods in it's own Constructor.



Problem: The Actual Code that the following Sample Code represents will Compile Successfully, but upon testing in the Javascript Console, it does not.



Sample:



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
}
}


Technical: The Typescript Compiler does not have a problem with this.setupAudioNodes() but once called as Javascript within the Browser's Javascript Console I receive an error Uncaught TypeError: undefined is not a function. Effectively, this is an issue with Javascript's this. syntax and how it's easy to get confused with it. But because I'm developing in Typescript, I want a more Typescript style solution.



Question: How can I call a Class's Methods from it's Constructor in Typescript?


More From » html

 Answers
5

Here's how to call a method from the constructor:



class Thing {
constructor(name: string) {
this.greet(name);
}

greet(whatToGreet: string) {
console.log('Hello, ' + whatToGreet + '!')
}
}

var x = new Thing('world'); // Prints Hello, world!

[#70911] Tuesday, May 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;