Skip to content

Instantly share code, notes, and snippets.

Last active March 30, 2023 01:59
Show Gist options
  • Save cowboy/4477847 to your computer and use it in GitHub Desktop.
Save cowboy/4477847 to your computer and use it in GitHub Desktop.
JavaScript: call invo-cursion?
// OOP
console.log( 'OHAI'.blink() );
// Call invocation
console.log('OHAI') );
// $ always makes things look awesome.
var $ =;
// Very explicit call invocation
console.log( $.call(String.prototype.blink, 'OHAI') );
// Very, very explicit call invocation, ie. call invo-cursion?
console.log( $.call($,$,$,$,$,$,$,$,$,$,$,$, String.prototype.blink, 'OHAI') );
// ^^^^^^^^^^^^^^^^^^^^^^^ "bonus" calls
// You can have fun with apply invocation and _ too.
var _ = Function.prototype.apply;
// Very, very explicit apply invocation, ie. apply invo-cursion.
console.log( _.apply(_,[_,[_,[_,[_,[_,[_,[_, [ String.prototype.blink, ['OHAI'] ]]]]]]]]) );
// ^^^^^^^^^^^^^^^^^^^^^^ "bonus" applies, and fun w/brackets ^^^^^^^^
Copy link

Hey, cool stuff, but can someone please explain me how's new Array(5) is equivalent to {length: 5}, and where's {length: 5} coming from in terms of Array constructor parameters if understand this right?

Copy link

Regarding Array(n):

When Array is called as a function rather than as a constructor,
it creates and initialises a new Array 

taken with courtesy from Annotated ECMAScript 5.1

Copy link

@vsternbach {length: 5} is an array-like for apply function

From MDN docs:


fun.apply(thisArg, [argsArray])


The value of this provided for the call to fun. Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.
An array-like object, specifying the arguments with which fun should be called, or null or undefined if no arguments should be provided to the function. Starting with ECMAScript 5 these arguments can be a generic array-like object instead of an array. See below for browser compatibility information.

Copy link

In case this helps anyone else: I was confused as to why you needed that last Number passed in as the 2nd arg to map. I know that map invokes the given callback with 3 args: currentVal, index and array, and that Number just takes 1 arg, so instead of directly using Number as the callback (it would receive the first arg currentVal (undefined)), we use as the callback, which swallows the first arg as the this value and instead invokes Number with the second arg index, which is what we want.

But it turns out call doesn't just magically know which function it was called on, it just executes whichever function is in it's this value (I believe). Normally that will indeed be the function it was called on (it is effectively an Object method - see the MDN article on this), but in this case, we pass into map, which is then stored internally simply as callback, so it is no longer an Object method, it is now just a simple function, so (I assume) it's this defaults to the global object, which it then tries to invoke as a function, which throws an error. So in this case the Number from is lost and never actually gets called. That is why we have to pass in Number as the 2nd arg to map because it is then set as the this for the callback (which in this case is, and so based on our past assertions, call will then invoke it.

As mentioned earlier in this thread: an interesting consequence of all this is that it doesn't matter how you invoke call - it works just as well if you do map(, Number).

Props to these articles for helping me understanding this: Some interesting aspects of “call” method in JavaScript and Partial Application in JavaScript.

Feedback welcome on these wild theories!

FYI if you remove the last Number argument, and instead run Array.apply(null, {length: 5}).map( then in Chrome you get the error: Uncaught TypeError: Array.apply(...).map is not a function, which is confusing. Anyone understand this?

Copy link

haocong commented Sep 7, 2016

@jackocnr For the last FYI, I thought it would be like this: when you pass to, the is no longer an Object method, it is now just a simple function, with the call missing this then JavaScript runtime call an undefined function and will throw an error.

Array.apply(null, {length: 5}).map( // TypeError: undefined is not a function

For better understanding, here is another example:

var call =;
call() // TypeError: call is not a function, undefined, 1) // 1, undefined, 'foo') // foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment