Cannot access anonymous function

2019-08-25 18:58发布

问题:

There are two environments which using the 3rd party library (BrowserPrint.js);

  1. WORKING ENV - JS and jQuery where 3party libraries are included simply in the <head> part of the document

    and the main function is called in

    $(document).ready(setup_web_print);
    
  2. NOT WORKING ENV - Angular, JS and jQuery

    where 3rd party libraries are included in component:

    import * as $ from 'jquery';
    import * as bp from '../../../content/js/BrowserPrint-1.0.4.js';
    

    and triggered in ngOnInit() lifecycle hook:

    ngOnInit() {
       $(document).ready(function () {
        ... 
       })
    ...
    }
    

    There is an error in console

    ReferenceError: finishedFunction is not defined
    at Object.t.getDefaultDevice (BrowserPrint-1.0.4.js:95)
    

so it seems it cannot access finishedFunction

var a = n("GET", e("available"));
        a && (finishedFunction = function(e) {
            response = e, response = JSON.parse(response);
            for (var n in response)
                if (response.hasOwnProperty(n) && response[n].constructor === Array) {
                    arr = response[n];
                    for (var i = 0; i < arr.length; ++i) arr[i] = new t.Device(arr[i])
                }
            return void 0 == o ? void r(response) : void r(response[o])
        }, i(void 0, a, finishedFunction, s), a.send())

Does anybody know how to fix this and why doesn't work with in second env?

回答1:

I really don't know how JavaScript is handling this comma-written code. But I think this is what you want, isn't it?

 if(a) {
    var finishedFunction = function(e) {
      if (response = e, "" == response) return void s(null);
      response = JSON.parse(response);
      var n = new t.Device(response);
      s(n)
    };
    i(void 0, a, finishedFunction, o);
    a.send();
 }