简体   繁体   中英

How can I combine these functions

I'm learning about JavaScript functions and I've written three, which are basically identical:

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.

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

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

First, set up three const variables:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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