Created
February 18, 2012 23:48
-
-
Save nakamura-to/1861338 to your computer and use it in GitHub Desktop.
Function.prototype.apply and Function.prototype.call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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