Console.log crashes when assigning a new variable?

So this works in firefox and opera, but not in chrome or IE.

        window.onload=function(){

            IMS=new Object();
            IMS.putLog=console.log;


            IMS.putLog('IMS Initialized...');

            IMS.putLog('Loading...');
            loadData(userName, passWord);
            IMS.putLog('Loaded...');
        };

Illegal Call Failure

I do not know why? any advice?

+4
source share
2 answers

The reason you are calling IMS.putLogis a variable thisfor the function IMS; the implementation console.logprobably expects what thiswill be console.

Here's a workaround:

IMS.putLog = console.log.bind(console);

This ensures that this consolewhen you call the log function.

Unfortunately, this will not work in IE <9, or possibly other browsers. I know that binddoes not work in PhantomJS, if that matters.

+2
source

: " TypeError: " Chrome

, console.log, . , Firefox Opera .

:

IMS.putLog = function(){
   console.log.apply(console, arguments); //any passed to IMS.putLog will get passed to console.log
};

, .

: , console.log Edit2: Brainfart -

+1

Source: https://habr.com/ru/post/1529309/


All Articles