1

I have a constructor class, it should take in two numbers as arguments and build an object with two props and a single method.

export class Context{
    public input1: number;
    public input2: number;

    Constructor(num1: number, num2: number) {
        this.input1 = num1; 
        this.input2 = num2;
    }

    display() {
        console.log(this.input1, this.input2); 
    }
}

I instantiate the class, and attempt to pass in two values as arguments as follows:

import { Context } from "./context";

class App {
    public static main() {
        let context: Context = new Context(2,7); 
        context.display();
    }
}

App.main();

However, my IDE is stating that the new Context call for instantiation does not expect any arguments to be passed into it, this is not correct and I do not understand why I am receiving this error.

Thanks

2
  • 2
    Why does your Constructor have an uppercase C? Try constructor Commented Oct 24, 2019 at 13:57
  • Yeah, that fixed it thanks. Commented Oct 24, 2019 at 14:01

1 Answer 1

2

That's because register is important.

Constructor from capital C is not recognized as class constructor function, I think it is treated as a method.

Therefore, there is no constructor defined for the Context class, so you reasonably getting an error... Just rename Constructor to constructor...

Sign up to request clarification or add additional context in comments.

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.