0

Create a class that can be called directly without new

class View {
    constructor() {
    }
}
new View();
View()

Similar to this

new String("")
String("")

How can I do this?

1
  • 3
    TypeScript is just a superset of JavaScript, which means you can just define class in the good old ES5 way (You can not call ES6 class constructor without new too). Commented Jun 6, 2019 at 3:44

1 Answer 1

1

What you are talking about is what's known as a factory function

https://medium.com/javascript-scene/javascript-factory-functions-with-es6-4d224591a8b1

class View{
    constructor(){
    }

    static makeView() {
        return new View();
    }
}
const view1 = new View();
const view2 = makeView();

I don't think you can call it the exact same thing as your class though.

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.