Skip to content

Javascript, remove jQuery call

I have a file.js like this:

// TICautocapture.js

var TICautocapture = (function(){
  var lib = {...}
  var error_handler;
  var handleError = (error_code, error_callback) => {...}
  function autocapture(container, options){...}

  return autocapture;
})();

if(window.jQuery){
  (function($){
    $.fn.autocapture = function(options){
      TICautocapture(this.attr('id'), options);
    }
  }(jQuery));
}

My question is (I’m not sure how jQuery works at this)

  • What is doing the last if statement?

My idea is to convert that all function in a React util like

const TICautocapture = () => {
  // all the code inside
}

export default TICautocapture;

And to use its methods and values, but I don’t understand what it is doing that last part that I said. Any help please.

Answer

What is doing the last if statement?

It is simply checking if jQuery exists in the window object since the $ alias is accessed in the following instruction. This is an example of IFEE. The code checks if window.jQuery exists, then passes it to the IFEE and catches it as $ in the parameter for that function.

I hope this answered your question.