Skip to content Skip to sidebar Skip to footer

Why Can't I Rename Console.log?

This seems like it should be pretty straightforward: var print = console.log; print('something'); // Fails with Invalid Calling Object (IE) / Invalid Invocation (Chrome) Why doesn

Solution 1:

Becase you are calling the method with the global object as the receiver while the method is strictly non-generic and requires exactly an instance of Console as the receiver.

An example of generic method is Array.prototype.push:

var print = Array.prototype.push;
   print(3);
   console.log(window[0]) // 3

You can do something like this though:

var print = function() {
     returnconsole.log.apply( console, arguments );
};

And ES5 provides .bind that also achieves the same as above:

var print = console.log.bind( console );

Post a Comment for "Why Can't I Rename Console.log?"