Pass a value to a specific parameter without caring about the position of the parameter [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

This question already has an answer here:

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.

回答1:

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); }


回答2:

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});


回答3:

function myFunction(parameters) {   ...   var x = parameters.myParameter; }  myFunction({myParameter: 123});


回答4:

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});


回答5:

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);


回答6:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!