kankai
6 months ago

Can i flexible change function name in javascript?

Posted 6 months ago by kankai

Hi guys,

I'm currently creating my own javascript library, so i started with ajax submit form and i need to make it have flexible result effect. Is there anyway to make function name replace with parameter?

function submitForm(){
  var parameterLength = arguments.length;
  var formID = arguments[0];
  var postUrl = arguments[1];
  var _func = arguments[2];
  
  //
  $(document).ready(function() {
    $(formID).submit(function (ev) {
        //
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            method: 'POST', // Type of response and matches what we said in the route
            url: postUrl, // This is the url we gave in the route
            data:$(formID).serialize(), // a JSON object to send back
          success: function(response){ // What to do if we succeed
              _func(response);
          },
          error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
            console.log(JSON.stringify(jqXHR));
            console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
          }
        });
        ev.preventDefault();
    });
  });
}


function result(_data){
  console.log(_data);
}

submitForm('#requestForm','home','result')

Something like this but i got an error said that _func is not a function. Anyway to make this works?

Please sign in or create an account to participate in this conversation.