所以这在 firefox 和 opera 中有效,但在 chrome 或 IE 中无效。
window.onload=function(){
IMS=new Object();
IMS.putLog=console.log;
IMS.putLog('IMS Initialized...');
IMS.putLog('Loading...');
loadData(userName, passWord);
IMS.putLog('Loaded...');
};
非法调用失败
不知道为什么?有什么建议吗?
最佳答案
原因是当你调用IMS.putLog
时,函数的this
变量是IMS
; console.log
实现可能指望 this
成为 console
。
解决方法如下:
IMS.putLog = console.log.bind(console);
这将确保调用日志函数时this
是console
。
不幸的是,这在 IE < 9 或其他浏览器中不起作用。我知道 bind
在 PhantomJS 中不起作用,如果这很重要的话。
关于javascript - console.log 在分配给新变量时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22081618/