如何在函数中选择赋值变量的参数? -Javascript

时间:2016-04-22 00:54:30

标签: javascript

给出正常的功能;

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

我想使用相同的函数,但只想将最后两个参数传递给它:

descriptions(cool : "john", alright : "jane"); //I would like a statement similar to this that works.
//should return "no one is awesome. jack is cool. jefferson is alright."

如何完成上述工作?

7 个答案:

答案 0 :(得分:3)

使用对象解构可以实现语法上不同但语义相似的东西

function descriptions({ awesome = 'no one', cool, alright }) {
    return awesome + " is awesome. " + cool + " is cool. " +
     + alright + " is alright";
}

然后,您只需使用具有相应属性的对象调用它:

descriptions({ cool: 'a', alright: 'b'});

答案 1 :(得分:1)

这在任何种类的ECMAScript(包括JavaScript)中都是不可能的。

理论上可以做一些事情,比如使用条件,自定义逻辑:

function(a,b,c){
   if(arguments.length === 1) {
      // we're in object mode;
      b = a.b
      c = a.c
      a = a.a || 'default';
   }
}

但这不是语言的内置部分。

这可能是 NOT ,例如:

function foo(a,b,c){return a/(b || 1) + c;}
foo({c:1,b:2,a:3})

还可以根据参数的数量有条件地定义值:

function say (a,b,c) {
   if(arguments.length === 2) {
      c = b;
      b = a;
      a = 'cat';
   }
   console.log('a ' + a + ' likes a ' + b + ' and a ' + c)
}
say('dog', 'bone', 'walk') // a dog likes a bone and a walk
say('mouse', 'bowl of milk') // a cat likes a mouse and a bowl of milk

答案 2 :(得分:1)

是的,你当然可以做到这一点! 如果没有提供变量,您可以使用许多开发人员用来将变量设置为默认值的聪明技巧。

function descriptions(awesome, cool, alright){
awesome = awesome || "";
if (awesome === "")
{
    return "no one" + " is awesome. " + cool + " is cool. " +
 + alright + " is alright";
}
else{
    return awesome + " is awesome. " + cool + " is cool. " +
 + alright + " is alright";
}

}
console.log(descriptions(undefined, "jack", "jefferson"));

这是工作代码。你也可以传递一个空字符串。

答案 3 :(得分:1)

在ECMAScript 6中,如果您更改参数以接收对象并利用destructuring assignment,则可以执行此操作。



function descriptions({awesome: awesome = "no one", cool: cool = "", alright: alright = ""} = {}) {
     return awesome + " is awesome. " + 
            cool + " is cool. " +
            alright + " is alright";
}


var res = descriptions({ cool: "john", alright: "jane" });


document.body.textContent = res;




所以我们有一个模拟命名参数的人。呼叫者唯一需要的是大括号。

当然浏览器支持有限,但可以使用转发器。

答案 4 :(得分:1)

您可以通过传递对象来执行此操作:

function descriptions(info) {
    // Avoid TypeError if no argument is passed
    if (!info) {
        info = {};
    }

    return (info.awesome || "no one") + " is awesome. " + (info.cool || "no one") + " is cool. " + (info.alright || "no one") + " is alright.";
}

// Use:
console.log(descriptions({
    awesome: "Strong Bad",
    cool: "The Cheat",
    alright: "Strong Sad"
}));

答案 5 :(得分:1)

您可以使用其他方法:

var coolLevels = {
  isCool: ["Jack", "John"]
, isAlright: ["Jane", "Jefferson"]
, isAwesome: []
}

function describe(people, coolLevel, phrase) {
  return people.filter(function(person){
    return Boolean(coolLevel.indexOf(person))
  }).join(", ") + phrase
}

function descriptions(people){
  var awesome = describe(people, coolLevels.isAwesome, ' is awesome.')
  var cool = describe(people, coolLevels.isCool, ' is cool.')
  var alright = describe(people, coolLevels.isCool, ' is alright.')

  return awesome + cool + alright
}

演示:https://jsbin.com/kahawuhelu/edit?js,console,output

答案 6 :(得分:1)

您可以将undefinednull""作为第一个参数传递。 E.g:

descriptions(null, "jack", "jefferson");

由于您已经使用awesome || "no one",因此任何虚假值都足够了。

另一种方法是更改​​函数以接收对象:

function descriptions(options) {
  return (options.awesome || "no one") + " is awesome. " + options.cool + " is cool. " + 
    options.alright + " is alright";
}

descriptions({ cool: "jack", alright: "jefferson" });

现在,根据您的浏览器支持,您可以使用ES6解构参数:

const descriptions = ({ awesome = 'no one', cool, alright }) => (
  `${awesome} is awesome. ${cool} is cool. ${alright} is alright`
);

descriptions({ cool: 'jack', alright: 'jefferson' });