Home > Blockchain >  How to correctly use objects instead of multiple parameters?
How to correctly use objects instead of multiple parameters?

Time:12-11

How to correctly use objects instead of multiple parameters in my case?

class Test{ 
async createUser(
    url: string,
    name: string,
    email: string,
    phone: string,
    street: string,
...
  ) {
    await Page.setUrl(url);
    await Page.setEmail(email);

 ...
   
  }
}

CodePudding user response:

you can use an interface


interface User {
  url: string;
  name: string;
  email: string;
  phone: string;
  street: string;
}

class Test{ 
async createUser(user: User) {
    await Page.setUrl(user.url);
    await Page.setEmail(user.email);

 ...
   
  }
}

CodePudding user response:

use the concept of spread operators. Know more about it here:

create an object variable like this:

result = { 
    url: string,
    name: string,
    email: string,
    phone: string,
    street: string,
}

and then pass it as a parameter like this

 async createUser (...result){
    //get variable you wan to use
    const {url, name, email} = result;
    //your code here
   }
  • Related