Skip to content

Instantly share code, notes, and snippets.

@kopiro
Created November 30, 2015 12:04
Show Gist options
  • Save kopiro/6000c4e1b399041107ac to your computer and use it in GitHub Desktop.
Save kopiro/6000c4e1b399041107ac to your computer and use it in GitHub Desktop.
Call and Apply replica in Pure JS
function x(a,b,c) {
console.log('Called X:', a,b,c);
}
Function.prototype._call = function() {
var first = Array.prototype.shift.apply(arguments);
this.apply(first, arguments);
};
Function.prototype._apply = function() {
var first = Array.prototype.shift.call(arguments);
var str = [];
for (var i = 0; i < arguments[0].length; i++) str.push('arguments[0]['+i+']');
eval('this.call(first,' + str.join(',') + ')');
};
x._apply(null, [1,2,3]);
x._call(null, 1,2,3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment