Is console.log a regular object? [duplicate] - javascript

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

Related

Structure of console prototype [duplicate]

This question already has answers here:
__proto__ VS. prototype in JavaScript
(34 answers)
Closed 6 years ago.
Probably it's because I'm misunderstanding the prototype chain, but could someone explain me the prototype structure that makes this assertion true?
console.log.prototype === console.prototype
I expect it to be something like this
console.prototype.log = function(){...}
So log has the basic function prototype. How does that prototype resolves to his parent prototype ?
I tried some things that I didn't expected to work, but they work. For example, instead of doing:
var binded = console.log.bind(console,'something');
I can do this
var otherBind = console.log.bind(console.log,'something else')
Neither console.log nor console are class constructors, so their prototype properties are undefined. Since undefined === undefined, console.log.prototype === console.prototype is true.
Check out Reflect.getPrototypeOf(), that might be what you're looking for.
console.log.bind(console.log) means that this method will be called with console.log as this value. Calling the bound function works fine on Chrome and Node.js, but fails on Firefox (TypeError: 'log' called on an object that does not implement interface Console.). See console.log() called on object other than console behaves differently among different browsers.

Error in Overriding alert with console.log function in javascript [duplicate]

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);

How come my code works in the codeacademy javascript console, but not the one in chrome? [duplicate]

This question already has an answer here:
Illegal Invocation error when console.log passed in a function
(1 answer)
Closed 7 years ago.
function forEach(array, action) {
for(i = 0; i <= array.length; i++) {
action(array[i]);
}
};
console.log(forEach([1, 2, 3], console.log));
How come this code executes the way I want it to in the codeacademy javascript console, but not the one in chrome? In chrome it keeps saying illegal invocation so I'm not sure if I'm doing some wrong or not. Any help please?
console.log expects this to be bound to console when you invoke it. (When you call it inside your forEach, you're not accessing it as a method of console anymore, so its internal this will be bound to the global object.)
Use console.log.bind(console) in place of console.log.

Why I cant assign a function like this [duplicate]

This question already has answers here:
alias to chrome console.log
(3 answers)
Closed 9 years ago.
Why I can't make this in javascript:
var log = console.log;
log(123);
And how to achieve that?
In JavaScript, the value of this depends on how a function is called.
Inside log(), this is undefined in strict mode or the default object otherwise (that's window in a browser).
Inside console.log(), this is console.
The log function, presumably, depends on having access to the console object.
You can write a wrapper function instead:
function log() {
console.log.apply(console, arguments);
}
In sufficiently modern browsers, you can generate that function using bind.
var log = console.log.bind(console);

what is an illegal invocation typeerror in javascript [duplicate]

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) };

Categories