11

I was wondering if it is possible to pass a value to a specific parameter by for example specifying its name, not taking into account if this parameter is the first, the second or the 100th one.

For example, in Python you can do it easily like:

def myFunction(x, y):
    pass

myFunction(y=3);

I really need to pass a value to a specific parameter of which I don't know necessarily its position in the parameters enumeration. I have searched around for a while, but nothing seems to work.

nbro
  • 15,395
  • 32
  • 113
  • 196
  • You can still invoke function by parameter names, assuming it's not system function and is not minimized. You can do it by explicitly looking at parameter names in function declaration. See my answer with working code. – dimakura Sep 30 '15 at 14:49
  • JavaScript is not Python. Use an object if you don't care about order, but names. And don't call functions whose signature you don't know at all. – Bergi Sep 30 '15 at 14:59
  • @Bergi Who said Javascript is Python? Javascript is a lot worse indeed (in general). – nbro Sep 30 '15 at 15:47
  • @Rinzler: You seem to want it to work like Python - but it won't, as JavaScript is different (not going to discuss whether to the worse or better). Named parameters go against its spirit, and - mostly - history. – Bergi Sep 30 '15 at 15:55
  • @Rinzler: I recommend to understand and then embrace the prototype inheritance - it's so much simpler and more flexible than classes :-) Btw, `type(float('nan'))` gives `float` in Python just as well, I don't see what's wrong with that. – Bergi Sep 30 '15 at 18:07
  • @Bergi The meaning of the statement that you show above `type(float('nan'))` is different from the meaning of the statement `typeof NaN`. In the first case, you care casting simply a string to a float, and of course the type is a float (if you instead try the equivalent statement in Python `type("nan")`, it returns ``, which makes perfect sense, since `"nan"` is a `str` (string) object). IMO, classes are a lot more intuitive than prototypes, and in general they help you to structure better the code for big projects. – nbro Sep 30 '15 at 18:21
  • I just think that Javascript is not a good language for serious big projects: it makes you lose more time to find hacks to solve the problems of the language than anything else. I would really that people stop using it and start using a language such as Dart, which is amazing. – nbro Sep 30 '15 at 18:23
  • @Rinzler: Yeah, it's closer to `typeof Number("NaN")` in JS (…whose result makes perfect sense). But I [found `float('nan')` to be the best Python approximation for the missing `NaN` constant](http://stackoverflow.com/q/19374254/1048572). Regardless, didn't I try to EOD? – Bergi Sep 30 '15 at 18:30

6 Answers6

13

No, named parameters do not exist in javascript.

There is a (sort of) workaround, often a complex method may take an object in place of individual parameters

function doSomething(obj){
   console.log(obj.x);
   console.log(obj.y);
}

could be called with

doSomething({y:123})

Which would log undefined first (the x) followed by 123 (the y).

You could allow some parameters to be optional too, and provide defaults if not supplied:

function doSomething(obj){
   var localX = obj.x || "Hello World";
   var localY = obj.y || 999;
   console.log(localX);
   console.log(localY);
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • What a pity. The problem with this solution is that I need to pass all parameters as properties of an object always... – nbro Sep 30 '15 at 14:10
  • No, not necessarily. See update. You could provide defaults for those parameters which are "optional" – Jamiec Sep 30 '15 at 14:12
5

Passing parameters this way isn't possible in JavaScript. If you need to do this, you're better off passing an object into your function, with properties set to represent the parameters you want to pass:

function myFunction(p) {
    console.log(p.x);
    console.log(p.y);
}

myFunction({y:3});
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
3
function myFunction(parameters) {
  ...
  var x = parameters.myParameter;
}

myFunction({myParameter: 123});
Igor
  • 15,833
  • 1
  • 27
  • 32
2

I dont think that feature exists in JavaScript

The alternative way is

function someFun(params) {
   var x = prams.x;
   ...
}

Function call will be like

someFun({x: 5});
rajuGT
  • 6,224
  • 2
  • 26
  • 44
0

No, but you perfectly can pass an object to achieve that.

var myFunction = function(data){
    console.log(data.foo); //hello;
    console.log(data.bar); //world;
}

myFunction({foo: 'hello', bar: 'world'});

EDIT

Otherwise, your parameters will be received by ORDER, rather than name.

var myFunction = function(foo, bar){
    console.log(foo); //bar;
    console.log(bar); //foo;
    //hyper confusing
}

var foo = 'foo',
    bar = 'bar';

//Passing the parametes out of order
myFunction(bar, foo);
Filipe Merker
  • 2,428
  • 1
  • 25
  • 41
0

In Javascript there are no named params.

But assuming you are not calling system or compressed function you can still invoke it by argument names.

Below is an idea of how to do it:

function myFunction(x, y) { return x + y }

function invokeByParams(func, params) {
  var text = func.toString()
  var i1 = text.indexOf('(') + 1
  var i2 = text.indexOf(')')
  var args = text.substring(i1, i2).split(',')
  args = args.map(function(x) { return x.trim() })
  args = args.map(function(x) { return params[x.trim()] })
  return func.apply(this, args)
}

invokeByParams(myFunction, { y: 1, x: 2 }) // => 6
dimakura
  • 7,575
  • 17
  • 36