This question already has answers here:
Uncaught TypeError: Illegal invocation in JavaScript
(2 answers)
Closed 6 years ago.
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.
Hope this should work...
alert("msg");
function alert(msg){
console.log(msg);
}
This should do
var alert = (...params) => console.log(...params);
Related
This question already has answers here:
What is the difference between a function call and function reference?
(6 answers)
Closed 2 years ago.
Recently I started learning about JS, and I got stuck in the following issue:
I am trying to call a function "draw_point" when a "mousemove" event happens.
When trying the following, the code works as expected (the function gets called):
svg.on('mousemove', draw_point(true));
But when replacing the caller function like this, it stops working and I don't get any error messages to troubelshoot.
svg.on('mousemove', function () {
draw_point;
});
Any explanation of what is going on?
Both examples are wrong, but for different reasons. In the first, you're calling the functiondraw_point immediately and passing its return value to the .on call. In the second, you're creating an anonymous function that wraps draw_point, but you're not calling draw_point. This is legal because javascript allows empty statements like myVar; or 3;.
What you want to do is pass the function draw_point, but not call it. It will be called when the handler runs:
svg.on('mousemove', draw_point);
This question already has answers here:
How to disable JavaScript function calls from the browser console?
(6 answers)
Closed 3 years ago.
Alright so I've a function (not exactly the one written bellow, that one is just an example)
function test()
{
alert("Hello");
}
Now the problem is that people are able to go into console and just type "test()" and call the function.
Is there a way to prevent this? I dont want of people to call functions from the console.
You can narrow the context that your function is executed in, eg:
(function() {
function test() { /* Do anything */ }
test();
// Call test anywhere here
})()
test(); // Error: test is undefined
This question already has answers here:
Create shortcut to console.log() in Chrome
(4 answers)
Uncaught TypeError: Illegal invocation in JavaScript
(2 answers)
Illegal Invocation error when console.log passed in a function
(1 answer)
Closed 8 years ago.
I have javascript code that raises an alert if it's being run in the browser, but which I don't want to raise alerts when I run unit tests.
I tried to solve this by having a line
if( allowAlerts === false ){
alert = console.log;
}
but when I then run
alert("This bad thing happened");
I get back
TypeError: Illegal invocation
directly reassigning alert was a kludgey solution, and I can easily solve the problem in other ways, but I've never come across an illegal invocation error before and was hoping someone could explain what it means.
The console.log function needs its calling context to be the console.
Use
alert = console.log.bind(console);
Or if you want to be compatible with old IE :
alert = function(){ console.log.apply(console, arguments) };
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
If Javascript has first-class functions, why doesn’t this work?
In Chrome, the following produces Uncaught TypeError: Illegal invocation:
g = console.log;
g(1);
Why does this happen, and why can't I treat console.log like a regular object?
It happens because you've lost the reference to console. You're just calling log directly, with no context. You can call the function in the context of console to make it work:
g.call(console, 1);
Or, to save you from doing that every time, you can bind the function back to the console object:
var g = console.log.bind(console);
References:
Function.prototype.call
Function.prototype.apply (not used here, but still of interest)
Function.prototype.bind
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.