Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created February 18, 2012 23:48
Show Gist options
  • Save nakamura-to/1861338 to your computer and use it in GitHub Desktop.
Save nakamura-to/1861338 to your computer and use it in GitHub Desktop.
Function.prototype.apply and Function.prototype.call
function f(a, b, c) {
console.log(this);
console.log(a);
console.log(b);
console.log(c);
}
var context = {
name: 'context'
};
console.log('---Function.prototype.apply.apply---');
Function.prototype.apply.apply(f, [context, [1, 'a', true]]);
console.log('---Function.prototype.apply.call---');
Function.prototype.apply.call(f, context, [1, 'a', true]);
console.log('---Function.prototype.apply.bind---');
Function.prototype.apply.bind(f)(context, [1, 'a', true]);
console.log('---Function.prototype.call.apply---');
Function.prototype.call.apply(f, [context, 1, 'a', true]);
console.log('---Function.prototype.call.call---');
Function.prototype.call.call(f, context, 1, 'a', true);
console.log('---Function.prototype.call.bind---');
Function.prototype.call.bind(f)(context, 1, 'a', true);
// result
/*
---Function.prototype.apply.apply---
{ name: 'context' }
1
a
true
---Function.prototype.apply.call---
{ name: 'context' }
1
a
true
---Function.prototype.apply.bind---
{ name: 'context' }
1
a
true
---Function.prototype.call.apply---
{ name: 'context' }
1
a
true
---Function.prototype.call.call---
{ name: 'context' }
1
a
true
---Function.prototype.call.bind---
{ name: 'context' }
1
a
true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment