JavaScript Create a generic function create generic function java,create generic function c#,create generic

I've got a load of very similar functions that do different things I'm trying to refactor this. Below I've created a generic_command:

function generic_command(name, run){
  var start_position = $('textarea').prop('selectionStart'),
      end_position = $('textarea').prop('selectionEnd'),
      current_command = { command : name, last_start_position : start_position, last_end_position : end_position }

  if(current_command == last_command){

    original_content = $('textarea').val()

    run // different code needs to be executed here!

    last_command = current_command

  }
}

Literally the only thing that changes in the function is the code that is run at run.

I tried to call it like this:

$('#remove_bold').bind('click', function(){
  generic_command('remove_bold', remove_character(start_position), remove_character(end_position - 2 ))
});

However, this doesn't work as start_position and end position are undefined when called by the listener.

I'm guessing it would work if I evaluated run as in eval(run), but I don't want to use eval as this is insecure.

Should I create a generic_command class and make instances of it? How should I do that? Could I have an example?

Answer:1

You need to pass a callback function:

function generic_command(name, run){
    var start_position = $('textarea').prop('selectionStart'),
        end_position = $('textarea').prop('selectionEnd'),
        current_command = { command : name, last_start_position : start_position, last_end_position : end_position };
    if(current_command == last_command){
        original_content = $('textarea').val();
        run(start_position, end_position);
        last_command = current_command;
    }
}

generic_command("remove_bold", function(start_position, end_position) {
    remove_character(start_position), remove_character(end_position - 2 );
});
Answer:2

You can solve this problem by thinking functionally. The approach you can use is to pass in a function that takes the needed values as parameters, and then executes on them whatever you need. Like this:

// run should be a function that takes a command object as its parameter
function generic_command(name, run) {
    var start_position = $('textarea').prop('selectionStart'),
        end_position = $('textarea').prop('selectionEnd'),
        current_command = { 
            command: name, 
            start_position: start_position, 
            end_position: end_position 
        };

    if (current_command == last_command) {    
        original_content = $('textarea').val();

        run(current_command);

        last_command = current_command;    
    }
}

Then you can implement your event handler like this:

$('#remove_bold').bind('click', function(){
    generic_command('remove_bold', function(command) {
        remove_character(command.start_position);
        remove_character(command.end_position - 2 );
    });
});

Incidentally, I think you still have a logic issue here because in the above function current_command == last_command will always be false.

Answer:3

I have this function: function fun_voting_started(){ var now = new Date(); var now_date = now.getDate(); var now_month = now.getMonth(); now_month++; var now_year = now.getFullYear(); var now_hours = ...

I have this function: function fun_voting_started(){ var now = new Date(); var now_date = now.getDate(); var now_month = now.getMonth(); now_month++; var now_year = now.getFullYear(); var now_hours = ...

  1. current date from sql server
  2. current date from dual
  3. current date from moment
  4. current date from calendar java
  5. current date from db2
  6. current date from javascript
  7. current date from mysql
  8. current date from oracle
  9. current date from sql
  10. current date from java
  11. current date from python
  12. current date from hive
  13. current date from php
  14. current date from vba
  15. current date from teradata
  16. current date from carbon
  17. current date from timestamp
  18. current date from
  19. current date in excel
  20. current date in c#

Is there a easy way to find all js events that are associated with a specific HTML element using Chrome? Example: HTML element <a href="#" class="cancel_link refresh_page">Cancel</a> ...

Is there a easy way to find all js events that are associated with a specific HTML element using Chrome? Example: HTML element <a href="#" class="cancel_link refresh_page">Cancel</a> ...

I wants browser to download a video as file as soon as when server is ready. I have seen Download File Using Javascript/jQuery , however,It is not possible to change MIME on my server. But some ...

I wants browser to download a video as file as soon as when server is ready. I have seen Download File Using Javascript/jQuery , however,It is not possible to change MIME on my server. But some ...

  1. download file with curl
  2. download file with powershell
  3. download file with python
  4. download file with wget
  5. download file with requests python
  6. download file with javascript
  7. download file with ssh
  8. download file with ajax
  9. download file with requests
  10. download file with fetch
  11. download file with cmd
  12. download file with axios
  13. download file with scp
  14. download file with postman
  15. download file with curl mac
  16. download file with nodejs
  17. download file with terminal
  18. download file with url
  19. download file with selenium python
  20. download file with certutil

Unfortunately my front end skills are lacking as my role puts me more on the server side / db technologies as opposed to css / js. In any event, I am trying to implement this: https://github.com/...

Unfortunately my front end skills are lacking as my role puts me more on the server side / db technologies as opposed to css / js. In any event, I am trying to implement this: https://github.com/...