Skip to content

Instantly share code, notes, and snippets.

@fakefarm
Created August 29, 2014 22:06
Show Gist options
  • Save fakefarm/97e8e81c02a2630939e2 to your computer and use it in GitHub Desktop.
Save fakefarm/97e8e81c02a2630939e2 to your computer and use it in GitHub Desktop.
call, bind, apply

Notes from Eloquent JavaScript

Recall that the apply and bind methods both take a first argument that can be used to simulate method calls. This first argument is in fact used to give a value to this.

There is a method similar to apply, called call. It also calls the function it is a method of but takes its arguments normally, rather than as an array. Like apply and bind, call can be passed a specific this value.

speak.apply(fatRabbit, ["Burp!"]);
// → The fat rabbit says 'Burp!'
speak.call({type: "old"}, "Oh my.");
// → The old rabbit says 'Oh my.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment