1

Need help to write following javascript code in typescript, it is a simple class with two public functions:

var Myclass =   (function  ()
{
var teststr = ['ein test','noch ein test'];

function Myclass (){
    this.test();
}
function getStr (id) {
    return teststr[id];
}
Myclass.prototype.test = function ()
{
    console.log(getStr(0))
    this.test1();
}
Myclass.prototype.test1 = function ()
{
    console.log(getStr(1))
}
return Myclass;
})();
var instMyClass = new Myclass();
  • var instMyClass call the constructor , than the constructor call public function test.
  • function test give out the first element of private array teststr and call public function test1
  • function test1 give out the second elemement of private array teststr

i try this solution, bur typescript compilier shows errors

class Myclass {
private teststr:Array<string> = ['ein test','noch ein test'];
constructor() {
    this.test();
}
function getStr() {
    return teststr[id];
}
test() {
console.log(getStr(0));
    this.test1();
}
test1(str:string) {
console.log(getStr(1));
}
}

let instMyclass = new Myclass();

if i try a private function with a form.submit, than the function is undefined:

class Ticket {

private form: HTMLFormElement;

constructor() {

    this.form = document.forms[0]!
    this.form.onsubmit = this.submit;
}
private setUser (user: TicketUser):void {
    console.log('ticket setUser',user);
}
public submit ():any {
    console.log('ticket submit');
    this.setUser({name:'stefan',age:100});
    return false;
}
}
9
  • Read the docs: typescriptlang.org/docs/handbook/classes.html Commented Sep 23, 2017 at 14:20
  • Have you tried it yourself? Commented Sep 23, 2017 at 14:39
  • @jonrsharpe, i read the docs, but my english is small so i thought it is a good idea to ask someone. Commented Sep 23, 2017 at 14:50
  • SO is not a tutorial service; there are many tutorials and articles out there on TS, you may even find some in your native language. Commented Sep 23, 2017 at 14:51
  • 1
    So show us what you've tried, what errors you got. Ask specific questions instead of asking people to write code for you. Commented Sep 23, 2017 at 15:01

3 Answers 3

0

Can you try with the typescript code below . Updated my answer

ts file

class Ticket {

    private form: HTMLFormElement;

    constructor(elem: HTMLFormElement) {

        this.form = elem;
        //this.form.onsubmit = this.submit();
    }
    private setUser(user: TicketUser): void {
        console.log('ticket setUser', user);
    }
    public submit(): any {
        console.log('ticket submit');
        this.setUser({ name: 'stefan', age: 100 });
        return false;
    }
}

class TicketUser {

    name: string;
    age: number;
}

window.onload = () => {
    var btn = document.getElementById("btn");
    btn.onclick = function () {
        var form = document.forms[0];
        var ticket = new Ticket(form);
        form.onsubmit = ticket.submit();

    }
};

HTML

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />

</head>
<body>
    <h1>TypeScript test</h1>

    <form id="myform" action="#"></form> <br />
    <button id="btn">click me</button>
    <script src="test.js"></script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

9 Comments

Thx, this was my stupid error, i did not understand wath's privat and what is public, thx nilardi that you are open my eyes....
it only works if i call the member functions from out of th constructor, but if the call comes from a form.submit, than the private function is undefined. please see Example in my updated posting
@S.Chrobak what happens if you make it public ?... the code I provided above is not related to HTMLFormElement, this was related to typescript. But you have changed your question . it's better that you post a different question with the updated code you are using.
@S.Chrobak where are you getting the undefined error?
@Niardi, sorry for posting update! When i start today i was little bit confuse, so i tryed to ask with a simple code , but didn't realizied that when the meber function call come from a dom event it si not the same as to call this function from construct.
|
0

You can use getter/setter for what you are trying to do.
Firstly you forgot the 'this' to call variables and functions withing class.
Secondly you don't use function, you use private, publict and protected to declare them.
See the example below on how to build, instantiate and use a class.

class Myclass {
private teststr: Array<string> = ['ein test','noch ein test'];

constructor() {
    this.test();
}

public getStr(id): string {
    return this.teststr[id];
}

public setStr(str: string) {
    this.teststr.push(str);
}

private test() {
    console.log('This works, calling withing class functions within class');
}

private test1(str:number) {
    console.log(this.getStr(1));
}
}

let instMyclass = new Myclass();
instMyclass.setStr('Another String');
let x = instMyclass.getStr(2);
console.log(x);

If you have tsc compiler install do this:

tsc myfilename.tsc
node myfilename.js

and you should see the output.

Comments

0

You can now use private access fields in TypeScript:

class Person {
  #address = '221B, Baker Street, London';
  
  checkAddress(address: string) {
      return this.#address === address;
  }
}

Read more about this in my article here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.