Thomas Stock
Thomas Stock

Reputation: 11296

Using an object for function parameters

var foo = function(a, b, c) {

  console.log(a);
  console.log(b);
  console.log(c.id);

};

//this works obviously
foo("a man", "b man", {id: "c.id man"}); 

var par = {
  a: "a val",
  b: "b cal",
  c: {
    id: "c.id val"
  }
};


//can I make this work automatically?
foo(par);

Question is in code sample.

Can I automatically "unwrap" the par object to use the properties to fill in the function parameters?

Is there some kind of foo(par.unwrap()) in javascript?

http://plnkr.co/edit/Y5M3qq7ROfaGGXrU21G5?p=preview

Upvotes: 4

Views: 131

Answers (6)

Hrishi
Hrishi

Reputation: 7138

You can do that if you use the ES-6 arguments deconstruction. The way to do it would be to define the function like

function foo({a, b, c}) {
  //here just use a, b, c as you want;
}

var par = {
  a: "a val",
  b: "b cal",
  c : {
  id: "c.id val"
 }
};
foo(par);

You can see the demo here

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382454

You can, but given that object properties are unordered it's a little hacky. The solution is to parse the function as a string to get the name of the parameters.

Here's a utility function :

function callunwrap(f,o){
    f.apply(null, (f.toString().match(/\([^\)]*\)/)[0].match(/\w+/g)||[]).map(function(name){
        return o[name];
    }));
}

Usage example

var foo = function(a, b, c) {
  console.log(a);
  console.log(b);
  console.log(c.id);
};

var par = {
  a: "a val",
  b: "b cal",
  c: {
    id: "c.id val"
  }
};


callunwrap(foo, par);

Now, as you see, callunwrap does a lot of things, I wouldn't recommend to use such a hacky thing in a real program. The usual non hacky solution is to have your function explicitly read the arguments :

var foo = function(o) {
  console.log(o.a);
  console.log(o.b);
  console.log(o.c.id);
};

Upvotes: 4

Shoaib Chikate
Shoaib Chikate

Reputation: 8973

You can create upwrap() method for that Object.

var foo = function(a,b,c) {

  console.log(a);
  console.log(b);
  console.log(c.id);

};

 var par = {
  a: "a val",
  b: "b cal",
  c: {
    id: "c.id val"
  },
  unwrap:function(){
    var valueArray=[];
    for(key in this){
      if(typeof this[key]!=="function"){
        valueArray.push(this[key]);
      }

    }
    return valueArray;
  }
};



foo("a man", "b man", {id: "c.id man"}); 
foo.apply(null,par.unwrap())

Updated plunker

Upvotes: 0

Max
Max

Reputation: 1131

You can try

var parWorking = ["a val", "b cal", {
    id: "c.id val"
  }
];
foo.apply(this, parWorking);

Upvotes: 1

filur
filur

Reputation: 1576

Pretty easy to do it yourself?

var args = [];
for(var p in par){
    args.push(par[p]);
}
console.log(args); // ["a val", "b cal", Object { id="c.id val"}]

foo.apply(this, args);

http://jsfiddle.net/9fzg3ehu/

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

JavaScript argument names can be arbitrary and irrelevant, so it doesn't attach too much importance to them. In other words, you cannot set specific arguments based on name. More important are their indexes.

If you create an array out of the object's values, you can use foo.apply() to pass them in:

foo.apply(null /* the context */, parAsArray);

Upvotes: 0

Related Questions