How to choose what argument a variable is assigned to in a function? -Javascript -


given normal function such;

function descriptions(awesome, cool, alright){    return (awesome || "no one") + " awesome. " + cool + " cool. " +      + alright + " alright"; } descriptions("jane", "jack", "jefferson"); //returns "jane awesome. jack cool. jefferson alright." 

i use same function, pass final 2 arguments so:

descriptions(cool : "john", alright : "jane"); //i statement similar works. //should return "no 1 awesome. jack cool. jefferson alright." 

how above done?

you can passing object:

function descriptions(info) {     // avoid typeerror if no argument passed     if (!info) {         info = {};     }      return (info.awesome || "no one") + " awesome. " + (info.cool || "no one") + " cool. " + (info.alright || "no one") + " alright."; }  // use: console.log(descriptions({     awesome: "strong bad",     cool: "the cheat",     alright: "strong sad" })); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -