Skip to content

Instantly share code, notes, and snippets.

@chintamanil
Forked from branneman/call-apply-bind-proxy.js
Last active August 29, 2015 14:22
Show Gist options
  • Save chintamanil/c6a9b13e89ca08b846b4 to your computer and use it in GitHub Desktop.
Save chintamanil/c6a9b13e89ca08b846b4 to your computer and use it in GitHub Desktop.
var fn = function(arg1, arg2) {
var str = '<p>aap ' + this.noot + ' ' + arg1 + ' ' + arg2 + '</p>';
document.body.innerHTML += str;
};
var context = {
'noot': 'noot'
};
var args = ['mies', 'wim'];
// Calls a function with a given 'this' value and arguments provided individually.
// Support: everywhere
fn.call(context, args[0], args[1]);
// Calls a function with a given 'this' value and arguments provided as an array
// (or an array like object).
// Support: everywhere
fn.apply(context, args);
// Creates a new function that, when called, has its 'this' keyword set to the
// provided value, with a given sequence of arguments preceding any provided
// when the new function was called.
// Support: ECMAScript >= 5 (thus >= IE9)
var boundFn1 = fn.bind(context, args[0], args[1]);
boundFn1();
// Same as bind()
// Support: same as your jQuery version, available since 1.4
var boundFn2 = $.proxy(fn, context, args[0], args[1]);
boundFn2();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment