1

I have a constructor that looks like

constructor(values: Object = {}) {
        //Constructor initialization
        Object.assign(this, values);
    } 

but it requires named initialization :

new Inventory({ Name: "Test", Quantity: 1, Price: 100 })

can it be done directly from array like this:

new Inventory(["Test",1,100])

2 Answers 2

1

Try this

var [Name, Quantity, Price] = ["Test", 1, 100];
new Inventory({Name, Quantity, Price});
Sign up to request clarification or add additional context in comments.

Comments

0

You could change constructor to:

constructor(name: string, quantity: number, price: number) {
    //Constructor initialization
    Object.assign(this, { Name: name, Quantity: quantity, Price: price });
} 

1 Comment

Thanks a lot , but is it possible to use implicit initialization in both cases ,in case of many parameters it's too complicated

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.