Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinkelly/bf0e97002f4ebb02cded to your computer and use it in GitHub Desktop.
Save eoinkelly/bf0e97002f4ebb02cded to your computer and use it in GitHub Desktop.
// I am confused about how you can use `call` without supplying an object for `this`
// The docs don't indicate that you can:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
var funny = function (a) { return (a + 3); }
funny(3); // 6
funny.call({},3); // 6
funny.call(3); // NaN // (as expected)
var xs = [1,2,3]
var pop = Array.prototype.pop;
pop.call(xs); // 3 // Why does this work ...
pop.call({}, xs); // undefined // ... but this does not?
@chilts
Copy link

chilts commented Sep 9, 2014

This is fine since xs is your this arg:

pop.call(xs)

But when you call:

pop.call({}, xs)

... it is treating {} as your this arg. But that's not an array, therefore there is nothing to pop off. Also, the xs arg is ignored since pop() doesn't expect any arguments (only expects this).

@eoinkelly
Copy link
Author

Ah OK so I was misinterpreting pop as a "pull the last thing off the given array" when it is really "pull that last thing off the array you are attached to". Seems obvious in retrospect - thanks @chilts!

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