0

I am new to javascript. Due to certain reasons, I need to override windows.alert function by console.log function. For that, I have written following code,

window.alert        = console.log;
alert('abc'); // gives TypeError : Illegal invocation.

I don't know what's wrong I am doing here. As per my knowledge, it's possible to modify javascript function reference with another function object.

Edit

Many have downvoted my question and given me reference to similar question, but my problem is different. I want to suppress the warning of datatable grid library in jquery. That's why I want to replace alert by console.log or other function.

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97

2 Answers2

1

Hope this should work...

alert("msg");

function alert(msg){
  console.log(msg);
}
nisar
  • 1,055
  • 2
  • 11
  • 26
1

This should do

var alert = (...params) => console.log(...params);
Ademola Adegbuyi
  • 934
  • 1
  • 16
  • 25
  • Can you make me understand the above fancy code ? Or Can you give me any reference to it ? – Mangu Singh Rajpurohit Apr 13 '16 at 10:10
  • It's ES6. The alert function replaces the default alert function and ...params makes sure that whatever argument the log method can take, it mutates it in the alert function. I'm sure u know the log method can take more than an argument, so as the declared alert function. – Ademola Adegbuyi Apr 13 '16 at 11:11