如何选择在函数中为变量指定的参数-Javascript

如何选择在函数中为变量指定的参数-Javascript,javascript,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. j

给定一个正态函数

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."

如何实现上述功能?

这在任何种类的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';
   }
}
但这不是语言的固有部分

这是不可能的,例如:

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

这在任何种类的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';
   }
}
但这不是语言的固有部分

这是不可能的,例如:

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

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

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

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

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

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

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

使用对象解构可以实现语法不同但语义相似的功能

function descriptions({ awesome = 'no one', cool, alright }) {
    return awesome + " is awesome. " + cool + " is cool. " +
     + alright + " is alright";
}
然后使用具有相应属性的对象调用它:

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

使用对象分解可以实现语法上不同但语义上相似的功能

function descriptions({ awesome = 'no one', cool, alright }) {
    return awesome + " is awesome. " + cool + " is cool. " +
     + alright + " is alright";
}
然后使用具有相应属性的对象调用它:

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

在ECMAScript 6中,如果您更改参数以接收对象并利用它,则可以实现这一点

函数描述({awesome:awesome=“无人”,cool:cool=”“,ok:ok=”“}={}){
return awesome+“太棒了。”+
酷+“很酷。”+
好的+“是好的”;
}
var res=描述({cool:“john”,ok:“jane”});

document.body.textContent=res在ECMAScript 6中,如果您更改参数以接收对象并利用它,则可以完成此操作

函数描述({awesome:awesome=“无人”,cool:cool=”“,ok:ok=”“}={}){
return awesome+“太棒了。”+
酷+“很酷。”+
好的+“是好的”;
}
var res=描述({cool:“john”,ok:“jane”});

document.body.textContent=res您可以通过传递对象来执行此操作:

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"
}));
function descriptions(options) {
  return (options.awesome || "no one") + " is awesome. " + options.cool + " is cool. " + 
    options.alright + " is alright";
}

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

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

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"
}));
function descriptions(options) {
  return (options.awesome || "no one") + " is awesome. " + options.cool + " is cool. " + 
    options.alright + " is alright";
}

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

您可以使用不同的方法:

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
}

演示:

您可以使用不同的方法:

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
}

演示:

您可以将
未定义的
空的
作为第一个参数传递。例如:

descriptions(null, "jack", "jefferson");
既然您已经使用了
awesome | |“无人”
,任何虚假的值都足够了


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

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

您可以将
undefined
null
作为第一个参数传递。例如:

descriptions(null, "jack", "jefferson");
既然您已经使用了
awesome | |“无人”
,任何虚假的值都足够了


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

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


你不能。JS不支持命名参数。我应该删除我的问题吗?让我们看看社区的想法。这是一个有效的问题,“否”是一个完美的答案。您可以通过使用不同的数据结构和控制流来实现相同的结果。你有兴趣吗?当然有兴趣。你不能。JS不支持命名参数。我应该删除我的问题吗?让我们看看社区的想法。这是一个有效的问题,“否”是一个完美的答案。您可以通过使用不同的数据结构和控制流来实现相同的结果。你有兴趣吗?当然有兴趣。有解决办法吗?可能是涉及对象的东西?这是否假设a是传递给函数的对象?第一个示例假设第一个参数(
a
)是传递给函数的对象。但是,必须编写函数以预测该参数。是否有解决方法?可能是涉及对象的东西?这是否假设a是传递给函数的对象?第一个示例假设第一个参数(
a
)是传递给函数的对象。但是,函数的编写必须能够预见到该参数。你知道,在过去两年的大部分时间里,远离JS的一些新型ES6东西开始看起来像是深层魔法。如果通过{awesome:“someone”},上述函数会工作吗?将“某人”设置为“了不起”。@JasonBasanese其他属性如何?在当前实现中,其他函数没有默认值{@zerkms我想我对你的函数在做什么有些困惑。你介意详细说明一下吗?你知道,在过去两年的大部分时间里,远离JS,一些新的ES6东西开始看起来像是深魔法。如果通过{awesome:“someone”}以上函数会工作吗?将awesome设置为“someone”.@JasonBasanese其他属性如何?在当前实现中,其他属性没有默认值。同时,对该函数求值时抛出以下错误->未捕获SyntaxError:意外标记{@zerkms我想我对你的函数在做什么有些困惑。你介意详细说明吗?选择是为了简单。选择是为了简单。