-1

I have a couple questions about the below code:

  • What kind of function is this that has brackets on left side but no name before function ? Is this a special kind of function ?

  • Basically this function accepts callback as listeners, is it defined and run together ?

_

 (function(handlers) {
  Object.keys(handlers).forEach(function(observableName) {
    var observable = app.ko.observable().extend({
      notify: 'always'
    });
    that[observableName] = observable;
    var f = handlers[observableName];
    if (f) {
      observable.subscribe(f);
    }
  });
})({
  onCallIgnored: defaultCallHandler,
  onCallStateChanged: function(call) {
    that.videoState(call.getRemoteVideoState());
  },
  onPlacingCall: function(call) {
    callParams = app.utils.cloneObj(call);
  },
  onIncomingCall: function(call) {
    that.activeCall = call;
    callParams = app.utils.cloneObj(call);
    setCallEstablishingTimeout();
    app.playSound("ringtone");
    that.callProgressTime("");
  },
  onUpdateCallInfo: function(call) {
    console.log(call);
  }
});
epascarello
  • 204,599
  • 20
  • 195
  • 236
Rasim Avcı
  • 1,123
  • 2
  • 10
  • 16
  • https://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function – epascarello May 24 '18 at 12:32
  • 1
    Possible duplicate of [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – Richard-Degenne May 24 '18 at 13:01

2 Answers2

1

The function

(function(handlers) {
  ...
})()

is a self-executing anonymous function(also known as Immediately Invoked Function Expression). See this tutorial for good explanation.

When this code is executed by the JS interpreter it will define and call the function immediately with the set of handlers you are passing as parameters.

Community
  • 1
  • 1
mfilimonov
  • 587
  • 4
  • 18
1

What kind of function is this that has brackets on left side but no name before function ? Is this a special kind of function ?

The function is an anonymous function which is converted as a function expression by wrapping () around it. And by adding another (), you are invoking the function expression. Such invoked function expressions are knows as IIFE (Immediately Invoked Function Expression) which means you invoke the function as soon as it is defined.

e.g.

(function(x) {console.log(x);})(2); // paints 2

Basically this function accepts callback as listeners, is it defined and run together ?

Yes, the function is defined and run together. IIFE takes the values defined in () as arguments and pass it to the function.

(function(arg1, arg2, ...argN){ ... // function definition})(param1, param2, ... paramN);

So, the breakup of the IIFE will be as follows

function defintion

function(handlers) {
  Object.keys(handlers).forEach(function(observableName) {
    var observable = app.ko.observable().extend({
      notify: 'always'
    });
    that[observableName] = observable;
    var f = handlers[observableName];
    if (f) {
      observable.subscribe(f);
    }
  });
}

function argument 1 (handlers)

{
  onCallIgnored: defaultCallHandler,
  onCallStateChanged: function(call) {
    that.videoState(call.getRemoteVideoState());
  },
  onPlacingCall: function(call) {
    callParams = app.utils.cloneObj(call);
  },
  onIncomingCall: function(call) {
    that.activeCall = call;
    callParams = app.utils.cloneObj(call);
    setCallEstablishingTimeout();
    app.playSound("ringtone");
    that.callProgressTime("");
  },
  onUpdateCallInfo: function(call) {
    console.log(call);
  }
}
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59