Today I noticed something curious. It seems I canβt save the reference to the call property of the function and then execute it. Example:
var log = console.log; log.call(console, 'This works'); var logCall = console.log.call; logCall(console, 'This does not');
For me, this seems like perfectly legal Javascript, but the second call always gives me an error that is undefined is not a function . Feel free to play with him here , you will get the same results.
So why does Javascript stop me from calling call this way?
EDIT: I finally figured it out right in my head after reading SimpleJ. So I'm going to update this with how you can get the above to work:
var log = console.log; log.call(console, 'This works'); var logCall = console.log.call; logCall.call(console.log, console, 'This works now too');
The problem was that console.log getting the correct this value, but console.log.call not assigned the correct this value. So, as you can see, I basically had to execute console.log.call.call . Obviously, you would never use such code, I was just curious.
javascript
Gjk
source share