0

I want to write a javascript function that can flexibly add parameter. For detail, I want to encapsulate the singalR invoke function like below, but how should handle the parameter?
e.g. I want to call like this var proxy = initSignalr();
proxy.invoke('Login',name, pw, fn),
how should I adjust below code so I can make it flexible to add parameters.(e.g. maybe I also need to call proxy.invoke('sendMessage',message, fn) )

function initSignalr(){
  var connection = $.hubConnection('http://xxxxxxx');
  var proxy = connection.createHubProxy('xxxxxxx');


  connection.start().done(function(){
    console.log("signalr connected");
  });

  return {
    on: function (eventName, callback) {
          proxy.on(eventName, function (result) {
            $rootScope.$apply(function () {
              if (callback) {
                callback(result);
              }
             });
           });
         },
    invoke: function (methodName, callback) {
          proxy.invoke(methodName)
          .done(function (result) {
            $rootScope.$apply(function () {
              if (callback) {
                callback(result);
              }
            });
          });
        }
    };}
danny
  • 11
  • 2
  • 1
    Possible duplicate of [Is there a better way to do optional function parameters in Javascript?](http://stackoverflow.com/questions/148901/is-there-a-better-way-to-do-optional-function-parameters-in-javascript) – caballerog Dec 20 '15 at 21:36
  • http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – Andy K Dec 20 '15 at 21:42

1 Answers1

0
function test() {
  for(var i = 0; i < arguments.length; i++) {
      console.log(arguments[i]);
  }
}


test();

test('Some Text String');

test(5, 6, function() { console.log('test') })
Will
  • 1,718
  • 3
  • 15
  • 23