简体   繁体   English

我怎样才能结合这些功能

[英]How can I combine these functions

I'm learning about JavaScript functions and I've written three, which are basically identical:我正在学习 JavaScript 函数,我写了三个,它们基本相同:

function filterAll() {
    location.hash = "/"
}

function filterCompleted() {
    location.hash = "/completed"
}

function filterActive() {
    location.hash = "/active"
}

Rather than having three functions, is it possible to combine them and call the paramaters that I need at that time through one function name?与其拥有三个函数,不如将它们组合起来,通过一个函数名调用我当时需要的参数? This is how I see it in my head, but I can't seem to work it out:这就是我在脑海中看到的方式,但我似乎无法解决:

function filters(all, completed, active) {
    all = location.hash = "/";
    completed = location.hash = "/completed";
    active = location.hash = "/active";
}

filters(all);

Using an object literal as a simple map lookup you could do this->使用对象文字作为简单的地图查找,您可以这样做->

const actions = {
  all: '/',
  completed: '/completed',
  active: '/active'
}

function filter(action) {
  location.hash = actions[action];
}

//use like
filter('all');
filter('completed');
filter('active');

If you don't want to pass a string, another idea is using the map as an enum, to do this we could do these changes->如果您不想传递字符串,另一个想法是使用地图作为枚举,为此我们可以进行这些更改->

function filter(action) {
  location.hash = action;
}

//use like
filter(actions.all);
filter(actions.completed);
filter(actions.active);

You could use lots of consts like @Rounin mentions, but I'm not a fan of making more variables, even if they are scoped.你可以使用很多像@Rounin 提到的常量,但我不喜欢制作更多变量,即使它们是有范围的。

I'm not sure what you're trying to do, can you add some notes.我不确定您要做什么,您可以添加一些注释。 If it's to pass three variables then your last code sample is fine.如果要传递三个变量,那么您的最后一个代码示例就可以了。 IF its to pass one variable that has three data points, then use an object.如果它传递一个具有三个数据点的变量,则使用一个对象。

var filters = {all:"/", completed: "/completed", active: "active"};

then you can retrieve the values with (for example) alert(filters.all);然后您可以使用(例如) alert(filters.all);检索值alert(filters.all);

You can try this:你可以试试这个:

function filters(val) {
    switch (val) {
      case 'all':
       location.hash = "/";
       break;
      case 'completed ':
        location.hash = "/completed";
        break;
      case 'active':
       location.hash = "/active";
       break;
      default:
        break;
    }
    }

You can combine the three functions below into a single function which takes a single parameter.您可以将下面的三个函数组合成一个接受单个参数的函数。

Step One步骤1

First, set up three const variables:首先,设置三个const变量:

const all = '/';
const completed = '/completed';
const active = '/active';

Step Two第二步

Next, declare your function:接下来,声明你的函数:

function myFilter(filterType) {

  location.hash = filterType;
}

Step Three第三步

Now you can invoke one function using different values:现在您可以使用不同的值调用一个函数:

  • myFilter(all);
  • myFilter(completed);
  • myFilter(active);

It looks like this is what you are looking for.看起来这就是你要找的东西。

function filters(filterType) {
    if (filterType == 'all') location.hash = "/";
    if (filterType == 'completed') location.hash = "/completed";
    if (filterType == 'active') location.hash = "/active";
}

Although I would not recommend using your functions like this, this is how you would use the function:虽然我不建议您像这样使用您的函数,但您可以这样使用该函数:

filters('all'); // Set all filters
filters('completed'); // Set completed filters
filters('active'); // Set active filters

First You will need to define those variables as constant, just to used as parameters(action).首先,您需要将这些变量定义为常量,仅用作参数(动作)。

You may use that code Below您可以在下面使用该代码

function filters ( action ) {
 return action==="all" ? location.hash = "/" : action==="completed" ? location.hash = "/completed": action==="active" ? location.hash = "/active" : "No Match Found";
}

To test the function i provided a simple example:为了测试该功能,我提供了一个简单的示例:

let tod = "all"; 
let com = "completed"; 
let act = "active";

//Here you will see all the information you need. //在这里你会看到你需要的所有信息。 You can run it in your browser.您可以在浏览器中运行它。

[filters(tod),filters(com),filters(act)].forEach;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM